2016-04-22 168 views
2

這是我的示例文本模式MongoDB查詢嵌套文檔mongoDB

我想檢索每個用戶的數據。我也想要_id,因爲在一個完整的文檔中它有幾個具有類似結構的_id。

我嘗試使用開卷操作,因爲對象包含嵌套數組是這樣的:

db.getCollection('topic_stats_2').aggregate([{ $unwind : "$usages.type.users.text" }, { $unwind : "$usages.type.users.stats.xyz1" }, { $unwind : "$usages.type.users.stats.xyz2" }, { $unwind : "$usages.type.users.stats.xyz3" }, { $unwind : "$usages.type.users.xyz4" }, { $unwind : "$usages.type.users.xyz5" }]) 

但它提供了零次的結果。

我想要這樣的表格格式的結果。我知道表中將包含大量冗餘數據。但這是我想要的。

_id | count | xyz4 | xyz5 | xyz1 | xyz2 | xyz3 | text | type | 
    |  |  |  |  |  |  |  |  | 
    |  |  |  |  |  |  |  |  | 



/* 1 */ 
{ 
    "_id" : 「photo", 
    "count" : 236, 
    "usages" : [ 
     { 
      "type" : 1, 
      "users" : [ 
       { 
        "text" : 「jkncfkjfdn", 
        "stats" : { 
         「xyz1" : 6, 
         「xyz2" : 1, 
         「xyz3" : 1194 
        }, 
        「xyz4" : "julius babao", 
        「xyz5" : "juLiusbabao" 
       }, 
       { 
        "text" : 「fcnf", 
        "stats" : { 
         「xyz1" : 9, 
         「xyz2" : 6, 
         「xyz3" : 1199 
        }, 
        「xyz4" : "Dman", 
        「xyz5" : "DmanTheDesigner" 
       }, 
       { 
        "text" : 「dckejsndc", 
        "stats" : { 
         「xyz1" : 1, 
         「xyz2" : 0, 
         「xyz3" : 1200 
        }, 
        「xyz4" : "EastmanHouse", 
        「xyz5" : "EastmanHouse" 
       } 
      ] 
     }, 
     { 
      "type" : 2, 
      "users" : [ 
       { 
        "text" : 「msdnc", 
        "stats" : { 
         「xyz1" : 1, 
         「xyz2" : 1, 
         「xyz3" : 1168 
        }, 
        「xyz4" : "Shayne", 
        「xyz5" : "RKTay" 
       }, 
       { 
        "text" : 「kfjnvfv", 
        "stats" : { 
         「xyz1" : 0, 
         「xyz2" : 0, 
         「xyz3" : 523 
        }, 
        「xyz4" : "andy stitches", 
        「xyz5" : "myproudmendes" 
       }, 
       { 
        "text" : 「jkopoiuyt", 
        "stats" : null, 
        「xyz4" : "jm", 
        「xyz5" : "jihannelayosa" 
       } 
      ] 
     }, 
     { 
      "type" : 3, 
      "users" : [ 
       { 
        "text" : 「opted", 
        "stats" : { 
         「xyz1" : 58, 
         「xyz2" : 32, 
         「xyz3" : 1192 
        }, 
        「xyz4" : "♪♫Lil Darryl♫♪", 
        「xyz5" : "LilDarryl301" 
       }, 
       { 
        "text" : "Cloud 9", 
        "stats" : { 
         「xyz1" : 1, 
         「xyz2" : 1, 
         「xyz3" : 1171 
        }, 
        「xyz4" : "FGN", 
        「xyz5" : "pretty_brown66" 
       }, 
       { 
        "text" : "Cloud 9", 
        "stats" : { 
         「xyz1" : 0, 
         「xyz2" : 0, 
         「xyz3" : 997 
        }, 
        「xyz4" : "Travis Porter Jr .", 
        「xyz5" : "AyoTravo" 
       } 
      ] 
     }, 
     { 
      "type" : 4, 
      "users" : [ 
       { 
        "text" : 「while", 
        "stats" : { 
         「xyz1" : 1, 
         「xyz2" : 1, 
         「xyz3" : 1200 
        }, 
        「xyz4" : "LEGO Darth Vader", 
        「xyz5" : "LegoDarthVader" 
       }, 
       { 
        "text" : 「xjw", 
        "stats" : { 
         「xyz1" : 1, 
         「xyz2" : 1, 
         「xyz3" : 1198 
        }, 
        「xyz4" : "The Brothers Brick", 
        「xyz5" : "BrothersBrick" 
       }, 
       { 
        "text" : 「pol", 
        "stats" : { 
         「xyz1" : 1, 
         「xyz2" : 1, 
         「xyz3" : 1197 
        }, 
        「xyz4" : "BYTES & BRICKS", 
        「xyz5" : "lego_bb" 
       } 
      ] 
     }, 
     { 
      "type" : 5, 
      "users" : [ 
       { 
        "text" : 「qtwqyw", 
        "stats" : { 
         「xyz1" : 1, 
         「xyz2" : 1, 
         「xyz3" : 1155 
        }, 
        「xyz4" : "Kell_1976", 
        「xyz5" : "LuvsMyMunchkie" 
       }, 
       { 
        "text" : 「ytyty", 
        "stats" : { 
         「xyz1" : 12, 
         「xyz2" : 4, 
         「xyz3" : 1200 
        }, 
        「xyz4" : "carriewildes", 
        「xyz5" : "carriewildes" 
       }, 
       { 
        "text" : "from the high.", 
        "stats" : { 
         「xyz1" : 0, 
         「xyz2" : 0, 
         「xyz3" : 1067 
        }, 
        「xyz4" : "jake☄", 
        「xyz5" : "w0rshiptheking" 
       } 
      ] 
     } 
    ] 
} 

有人可以幫我在這個查詢嗎?

+0

從快看看你的$退繞看起來可疑:他們不是在陣列領域。 – joao

+0

@joao:你能告訴我如何放鬆嗎?或者更正查詢 – x0v

回答

1

您應用$unwind運算符的方式是錯誤的,因爲它只對數組字段起作用,並且您正在將其應用於非數組字段。您可以運行在聚集管道進行非規範化的陣列領域並獲得所需的結構:

db.getCollection('topic_stats_2').aggregate([ 
    { "$unwind": "$usages" }, 
    { "$unwind": "$usages.users" }, 
    { 
     "$project": { 
      "count": 1, 
      "xyz4": "$usages.users.xyz4", 
      "xyz5": "$usages.users.xyz5", 
      "xyz1": "$usages.users.stats.xyz1", 
      "xyz2": "$usages.users.stats.xyz2", 
      "xyz3": "$usages.users.stats.xyz3", 
      "text": "$usages.users.text", 
      "type": "$usages.type" 
     } 
    } 
]) 
+1

非常感謝。我認爲unwind也可以應用於數組的屬性。現在我懂了 – x0v