aggregate match时使用操作符是无效的
发布于 7 年前 作者 yan07 8346 次浏览 来自 官方Issues

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 回复

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

回到顶部