我在一个集合中有大量的记录,如下所示:
{
"_id" : ObjectId("5a95cf7790bd8fbf1c6a39da"),
"dmb_reviewerID" : "AB9S9279OZ3QO",
"dmb_asin" : "0078764343",
"dmb_reviewerName" : "Alan",
"dmb_helpful" : [
1,
1
],
"dmb_reviewText" : "I haven't gotten around to playing the campaign but the multiplayer is solid and pretty fun. Includes Zero Dark Thirty pack, an Online Pass, and the all powerful Battlefield 4 Beta access.",
"dmb_overall" : 5.0,
"dmb_summary" : "Good game and Beta access!!",
"dmb_unixReviewTime" : 1373155200,
"dmb_reviewTime" : "07 7, 2013"
}
我需要找到所有200条评论或以上的产品ID(dmb_asin)。
到目前为止,我已经设法计算它们并使用聚合返回一个总和,但我无法弄清楚如何仅显示大于200的那些。
我的代码:
aggregate({
$group: {
_id: "$dmb_asin",
reviews: {
$addToSet: "$dmb_asin"
},
count: {
$sum: 1,},
}
});
答案 0 :(得分:0)
尝试此查询:
db.collection.aggregate([
{$group: {
_id: "$dmb_asin",
reviews: {
$addToSet: "$dmb_asin"
},
count: {
$sum: 1,}
}},
{$match:{"reviews":{$gte:200}}}
])
答案 1 :(得分:0)
试试这段代码(如果我能正确理解你)
aggregate([
{
$group: {
_id: '$dmb_asin',
count: {
$sum: 1
}
}
},
{
$match: {
count: {
$gte: 200
}
}
}
])