2015-06-03 199 views
1

我有这两个MongoDB文档。我想找到真正的和巴萨的体育场馆的容量,假设皇马有两个场馆(对不起皇马:)嵌入文档数组 - MongoDB

{ 
     "_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来划分该数组的文档吗? 问题是巴塞罗那的场地球场不是一组文件,而且查询出现错误。

回答

0

您可能需要更改文档结构,因为stadium字段应该是每个文档中的for数组。要做到这一点,你需要找到的文件,其中球场不是一个数组,并使用“批量”操作的最大效率

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 }