我一直在寻找答案,但无法找到任何地方。查询返回扁平嵌入字段
当我在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