我为未指明的问题道歉,但我真的不知道它叫什么。
我们说我有"文件"属于"所有者"像这样的文件
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"
}]
我查找了它的doc并尝试使用find()和aggregate(),但似乎我遗漏了一些东西,所以它变成了"文档的数组"对象,但我不认为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 }}
])
答案 0 :(得分:0)
只是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);