嵌入式文档数组 - MongoDB

时间:2015-06-03 15:10:44

标签: mongodb mongodb-query aggregation-framework

我有这两个MongoDB文档。我想找到皇马和巴塞罗那体育场的容量,假设皇家马德里有两个体育馆(对不起Merengues:)

{
      "_id" : "Bar.43",
      "official_name" : "Futbol Club Barcelona
      "country" : "Spain",
      "started_by" : {
              "day" : 28,
              "month" : 11,
              "year" : 1899
      },
      "stadium" : {
              "name" : "Camp Nou",
              "capacity" : 99354
      },
      "palmarès" : {
              "La Liga" : 23,
              "Copa del Rey" : 27,
              "Supercopa de Espana" : 11,
              "UEFA Champions League" : 4,
              "UEFA Cup Winners Cup" : 4,
              "UEFA Super Cup" : 4,
              "FIFA Club World cup" : 2
      },
      "uniform" : "blue and dark red"
},

{
      "_id" : "RMa.103",
      "official_name" : "Real Madrid Club de Fùtbol
      "country" : "Spain",
      "started_by" : {
              "day" : 6,
              "month" : 3,
              "year" : 1902
      },
      "stadium" : [{
              "name" : "Santiago Bernabeu",
              "capacity" : 85454
      },
                   {
               "name" : "Vicente Calderon"
               "capacity" : 54907
      }],
      "palmarès" : {
              "La Liga" : 32,
              "Copa del Rey" : 19,
              "Supercopa de Espana" : 9,
              "UEFA Champions League" : 10,
              "UEFA Europa League" : 2,
              "UEFA Super Cup" : 2,
              "FIFA Club World cup" : 4
      },
      "uniform" : "white"
}

好吧,我的疑问是:

db.team.aggregate([{$group:{_id:"$country", capacityStadium:{$sum:"$stadium.capacity"}}}])

但它不起作用。如果皇马只有一个体育场,相反,我的查询有效。所以,一般来说,当我有一系列嵌入式文档并且我想使用聚合时,我是否必须使用$ unwind来划分该数组的文档? 问题是巴塞罗那的野外体育场不是一个文件阵列,查询得到错误。

1 个答案:

答案 0 :(得分:0)

您可能需要更改文档结构,因为stadium字段应该是每个文档中的数组。要做到这一点,你需要找到体育场不是阵列的文件,并使用“批量”操作更新它们以实现最高效率

var bulk = db.team.initializeOrderedBulkOp();
var count = 0;
db.team.find({ "stadium.0": { "$exists": false }}).forEach(function(doc) { 
    bulk.find({ "_id": doc._id })
        .updateOne({ "$set": { "stadium": [doc.stadium] }}); 
    count++; 
    if(count % 100 == 0) {     
    bulk.execute();     
    bulk = db.team.initializeOrderedBulkOp(); } })

if(count % 100 > 0) { 
    bulk.execute(); 
}

现在使用聚合框架。

db.team.aggregate(
    [
       { "$project": { "stadium.capacity": 1 }}, 
       { "$unwind": "$stadium" }, 
       { "$group": { "_id": "$_id", "totalCapacity": { "$sum": "$stadium.capacity" }}}
    ]
)

返回:

{ "_id" : "RMa.103", "totalCapacity" : 140361 }
{ "_id" : "Bar.43", "totalCapacity" : 99354 }