我为没有具体说明的问题道歉,但我真的不知道它叫什么。
假设我在“所有者”文档中有“文档”属性,如下所示
owner : [{
"_id": "1111",
"documents": [{
"file_name": "126789AP.pdf", "_id": "00A1"
},{
"file_name": "126789CE.pdf", "_id": "00A2"
},{
"file_name": "514132AP.pdf", "_id": "00A3"
}],
"name": "Dalton"
},{
"_id": "2222",
"documents": {
"file_name": "126789AP.pdf", "_id": "00B1"
},
"name":"Mason"
}]我需要像这样从每个所有者那里得到一个文档输出
documents : [{
"file_name": "126789AP.pdf", "_id": "00A1"
},{
"file_name": "126789CE.pdf", "_id": "00A2"
},{
"file_name": "514132AP.pdf", "_id": "00A3"
},{
"file_name": "126789AP.pdf", "_id": "00B1"
}]我已经查找了它的文档,并尝试了使用find()和聚合(),但是我似乎遗漏了一些东西,所以它变成了"documents“对象的数组,但是我认为mapReduce不是最好的方法,所以我寻求帮助。
编辑
这里是find()
db.owner.find({ "documents":{$exists:true} },{documents:1})以下是聚合()
db.owner.aggregate([{$project:{documents:1}},{$match:{documents:{$exists:true}}}])编辑2我终于想出来了!这是我的答案
db.students.aggregate([
{ $unwind : "$documents" },
{ $match : { "documents" : { $exists : true }}},
{ $project : { documents : 1 }}
])发布于 2016-02-11 08:21:22
只是javaScript中的一个基本循环
owner = [{
"_id": "1111",
"documents": [{
"file_name": "126789AP.pdf", "_id": "00A1"
},{
"file_name": "126789CE.pdf", "_id": "00A2"
},{
"file_name": "514132AP.pdf", "_id": "00A3"
}],
"name": "Dalton"
},{
"_id": "2222",
"documents": {
"file_name": "126789AP.pdf", "_id": "00B1"
},
"name":"Mason"
}];
var documents = owner.map(function(obj){
return obj.documents;
});
console.log(documents);https://stackoverflow.com/questions/35333937
复制相似问题