收藏
回答

aggregate match时使用操作符是无效的

框架类型 问题类型 终端类型 AppID 基础库版本
小程序 Bug 工具 wxfe1407b4a0142483 2.8.1

https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/aggregate/Aggregate.match.html

文档最后那行实际输出的结果是7,而不是3,说明aggregate match时使用操作符_.gt是无效的,等同没有此条件,实际返回所有数据。

{ "_id" : "1", "author" : "stark""score" : 80 }
{ "_id" : "2", "author" : "stark""score" : 85 }
{ "_id" : "3", "author" : "bob",    "score" : 60 }
{ "_id" : "4", "author" : "li",     "score" : 55 }
{ "_id" : "5", "author" : "jimmy""score" : 60 }
{ "_id" : "6", "author" : "li",     "score" : 94 }
{ "_id" : "7", "author" : "justan", "score" : 95 }
const db = wx.cloud.database()
const _ = db.command
const $ = _.aggregate
 
db.collection('article')
  .aggregate()
  .match({
    score: _.gt(80)
  })
  .group({
    _id: null,
    count: $.sum(1)
  })
  .end()

预期结果:

{ "_id" : null, "count" : 3 }

实际结果:

{ "_id" : null, "count" : 7 }



回答关注问题邀请回答
收藏

1 个回答

  • 老张
    老张
    2019-11-29

    我猜你的表里,score是string不是number

    2019-11-29
    有用
    回复 4
    • Scott Wan
      Scott Wan
      2019-11-29
      2019-11-29
      回复
    • Scott Wan
      Scott Wan
      2019-11-29
      确定是数字
      2019-11-29
      回复
    • 老张
      老张
      2019-11-29回复Scott Wan
      那就删除group阶段,直接看match的查询结果。
      2019-11-29
      回复
    • Scott Wan
      Scott Wan
      2019-11-29
      如果不要group阶段,输出的是该集合中的全部记录
      2019-11-29
      回复
登录 后发表内容
问题标签