我一直在尋找答案,但無法找到任何地方。查詢返回扁平嵌入字段
當我在robomongo中使用以下方法檢索匹配條件的文檔的嵌入字段時,我得到一個列表中的所有匹配文檔。 所有這些文檔都包含一系列帶有我選擇的嵌入字段的文檔。
db.getCollection('test').find(
{ "bom.COMPONENT": "101-00001-017" },
{ _id: 0, "CicodeList.Cicode": 1 }
)
結果
/* 1 */
{
"CicodeList" : [
{
"Cicode" : "one"
}
]
}
/* 2 */
{
"CicodeList" : [
{
"Cicode" : "two"
}
]
}
/* 3 */
{
"CicodeList" : [
{
"Cicode" : "three"
}
]
}
我想得到的是一個包含所有文件的所有嵌入式領域,像這樣一個數組:
{ results: ["one", "two", "three"] }
原始憑證看起來有點像這樣
/* 1 */
{
"_id" : ObjectId("583df12093181938d03c50eb"),
"CicodeList" : [
{
"Cicode" : "one"
},
{
"Cicode" : "two"
}
],
"bom" : [
{
"COMPONENT" : "101-00001-017"
},
{
"COMPONENT" : "101-00008-002"
}
]
}
/* 2 */
{
"_id" : ObjectId("583df12193181938d03c50ec"),
"CicodeList" : [
{
"Cicode" : "three"
},
{
"Cicode" : "four"
}
],
"bom" : [
{
"COMPONENT" : "101-00001-017"
},
{
"COMPONENT" : "101-00008-002"
}
]
}
看起來你必須使用聚合框架,該框架。或者只是讓你的應用程序代碼照顧它。 –
@wallage謝謝你的文檔,我已經更新了我的回答 –
[如何從mongoDB集合中獲取數組?](http://stackoverflow.com/questions/38210121/how-to-get-array-from- mongodb-collection) – styvane