【问题标题】:MongoDB grouping results based on greater than expressionMongoDB基于大于表达式的分组结果
【发布时间】:2018-04-06 16:19:26
【问题描述】:

我的集合中有大量记录,如下所示:

{
    "_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,},


}

});

【问题讨论】:

    标签: mongodb nosql mongodb-query


    【解决方案1】:

    试试这段代码(如果我理解正确的话)

    aggregate([
      {
        $group: {
          _id: '$dmb_asin',
          count: {
              $sum: 1
          }
        }
      },
      {
        $match: {
          count: {
              $gte: 200
          }
        }
      }
    ])
    

    【讨论】:

      【解决方案2】:

      试试这个查询:

      db.collection.aggregate([
            {$group: {
                   _id: "$dmb_asin",
                    reviews: {
                             $addToSet: "$dmb_asin"
                     },
                   count: {
                           $sum: 1,}
             }},
             {$match:{"reviews":{$gte:200}}}
      
      ])
      

      【讨论】:

      • 很遗憾没有返回任何结果
      • 请注意,您需要将collection 替换为您的集合名称。另外,如果可能,请导入您的收藏并分享 json,以便我们可以使用数据集尝试查看结果。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 2012-10-12
      • 1970-01-01
      • 2017-08-05
      相关资源
      最近更新 更多