收藏
回答

云数据库文档不严谨

云数据库文档不严谨啊,发现了好多处

新功能,让新手如何适应啊

毕竟是跟着例子走呢

1、Aggregate.bucket(object: Object): Aggregate

https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/aggregate/Aggregate.bucket.html#%E7%A4%BA%E4%BE%8B

output: {
  count: $.sum(1),
  ...
  : 
}

但在官方程序例中,却写成

db.collection('items').aggregate()
  .bucket({
    groupBy: '$price',
    boundaries: [0, 50, 100],
    default: 'other',
    output: {
      count: $.sum(),  // ---------- 错误, 应改为 $.sum(1)
      ids: $.push('$_id')
    }
  })
  .end()

返回结果如下:

 [
  {
    "_id": 0,
    "count": 2,
    "ids": [
      "1",
      "3"
    ]
  },
  {
    "_id": 50,
    "count": 2,
    "ids": [
      "2",
      "4"
    ]
  },
  {
    "_id": "other",
    "count": 22, // ---------- 错误, 应改为 1
    "ids": [
      "5"
    ]
  }
]


2、Aggregate.bucket(object: Object): Aggregate

https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/aggregate/Aggregate.bucket.html#%E7%A4%BA%E4%BE%8B

output: {
  count: $.sum(1),
  ...
  : 
}

但在官方程序例中,却写成

db.collection('items').aggregate()
  .bucket({
    groupBy: '$price',
    boundaries: [0, 50, 100],
    default: 'other',
    output: {
      count: $.sum(),  // ---------- 错误, 应改为 $.sum(1)
      ids: $.push('$_id')
    }
  })
  .end()

返回结果如下:

 [
  {
    "_id": 0,
    "count": 2,
    "ids": [
      "1",
      "3"
    ]
  },
  {
    "_id": 50,
    "count": 2,
    "ids": [
      "2",
      "4"
    ]
  },
  {
    "_id": "other",
    "count": 22,  // ---------- 错误, 应改为 1
    "ids": [
      "5"
    ]
  }
]


3、 Aggregate.bucketAuto(object: Object): Aggregate

https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/aggregate/Aggregate.bucketAuto.html#%E7%A4%BA%E4%BE%8B

db.collection('items').aggregate()
  .bucket({  // ---------- 错误, 应改为 bucketAuto
    groupBy: '$price',
    buckets: 3,  
  })
  .end()


4、 Aggregate.limit(value: number): Aggregate

https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/aggregate/Aggregate.limit.html#%E7%A4%BA%E4%BE%8B

db.collection('items').aggregate()
  .match({
    price: $.gt(20)  // ---------- 错误, 应改为 _.gt(20)
  })
  .sort({
    price: 1,
  })
  .limit(2)
  .end()

返回结果如下:【不知道哪里来的结果,根本不正确,正确的结果是:_id(2)与_id(4)】

 {
  "_id": "3",
  "price": 20
}
{
  "_id": "4",
  "price": 80
}


应该其他还有,拜托修正!

最后一次编辑于  2020-02-25
回答关注问题邀请回答
收藏
登录 后发表内容
问题标签