我已经能够创建一个聚合查询来按我的数据对象的字段进行计数。
db.getcollection.aggregate([
{"$group" : {"_id": "$attr","count": {"$sum" : 1}}},{"$sort": {"count": -1}}
])
这将导致基于{_id,count}
的文档以计数降序排序。到目前为止一切都很好。
我接下来要尝试的是filter
(匹配)仅计数超过 N
(例如100)个阈值的那些文档。但是当我执行时:
db.getcollection.aggregate([
{"$group" : {"_id": "$attr",{"$sort": {"count": -1}},{"$match": {"count": {$gte:["count",100]}}}
])
查询返回空结果。如何做到这一点?我也尝试使用$filter
而不是$match
来获得成功。