【问题标题】:How to count occurrences in nested document in mongodb?如何计算 mongodb 嵌套文档中的出现次数?
【发布时间】:2018-08-23 18:17:27
【问题描述】:

在数组中嵌套了数组的情况下,如何计算特定值的个数?例如,我想统计下面文档中“答案”的数量。查询应该返回有 2 个苹果和 1 个香蕉。

{
  "_id" : ObjectId("52c1d909fc7fc68ddd999a73"),
  "name" : "Some survey",
  "questions" : [
    {
      "_id" : ObjectId("52c1e250fc7fc68ddd999a75"),
      "answers" :
      [
        {
          "userId" : "some GUIDs",
          "answer" : "apple"
        },
        {
          "userId" : "some GUID",
          "answer" : "apple"
        },
        {
          "userId" : "some GUID",
          "answer" : "banana"
        }
      ],
      "questionText" : "blah blah blah...",
      "questionType" : "multiple choice"
    }
]

}

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    有几种方法可以解决此问题,具体取决于您需要处理的数据量。您可以在 MongoDB 2.2+ 中使用Aggregation Framework,或者可能使用Map/Reduce。有关功能和限制的摘要,请参阅 Aggregation Commands Comparison

    这是一个使用聚合框架的示例:

    db.fruit.aggregate(
        // Limit matching documents (can take advantage of index)
        { $match: {
            "_id" : ObjectId("52c1d909fc7fc68ddd999a73")
        }},
    
        // Unpack the question & answer arrays
        { $unwind: "$questions" },
        { $unwind: "$questions.answers" },
    
        // Group by the answer values
        { $group: {
            _id: "$questions.answers.answer",
            count: { $sum: 1 }
        }}
    )
    

    对于您的示例文档,这将返回:

    {
        "result" : [
            {
                "_id" : "banana",
                "count" : 1
            },
            {
                "_id" : "apple",
                "count" : 2
            }
        ],
        "ok" : 1
    }
    

    【讨论】:

      【解决方案2】:

      这是使用聚合框架为猫换皮的一种方法。一旦你学会了它,你就可以用你的数据做很多好事。

      db.so.aggregate([{$unwind:"$questions"}, {$unwind:"$questions.answers"}, {$group:{_id:"$questions.answers.answer", fruit_count:{$sum:1}}}])
      

      给我这个:

      {
          "result" : [
              {
                  "_id" : "banana",
                  "fruit_count" : 1
              },
              {
                  "_id" : "apple",
                  "fruit_count" : 2
              }
          ],
          "ok" : 1
      

      为了更加确定,我添加了这个文档:

      db.so.insert({
        "_id" : ObjectId("52c1d909fc7fc68ddd999a75"),
        "name" : "Some survey",
        "questions" : [
          {
            "_id" : ObjectId("52c1e250fc7fc68ddd999a75"),
            "answers" :
            [
              {
                "userId" : "some GUIDs",
                "answer" : "orange"
              },
              {
                "userId" : "some GUID",
                "answer" : "orange"
              },
              {
                "userId" : "some GUID",
                "answer" : "banana"
              }
            ],
            "questionText" : "blah blah blah...",
            "questionType" : "multiple choice"
          }
      ]
      })
      

      我的查询现在给了我:

      {
          "result" : [
              {
                  "_id" : "orange",
                  "fruit_count" : 2
              },
              {
                  "_id" : "banana",
                  "fruit_count" : 2
              },
              {
                  "_id" : "apple",
                  "fruit_count" : 2
              }
          ],
          "ok" : 1
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-30
        • 2021-08-28
        • 2020-02-08
        • 1970-01-01
        • 2012-08-03
        相关资源
        最近更新 更多