我有以下格式的文件嵌套:嵌套文檔中的MongoDB陣列讀取節點
{
_id: "1234567890",
course: {content: [{ id: 1,
children:[{
id: 1111
},
{
id: 2222
}
{
id: 3333
}]
},
{ id: 2,
children:[{
id: 4444
},
{
id: 5555
}
{
id: 6666
}]
}
]}
}
我試圖查詢基於ID(course.content.children.id)兒童使用以下從Node.js的代碼:
var query = {
'course.content.children.id': quizId,
};
var option = {
'course.content.children.$': 1,
};
db.getEntityWithOption(tablename,query,option,function(data,err){
res.send(data.course.content[0].children);
});
db.getEntityWithOption是db.find()方法的頂部包裝方法。當我打電話與測驗ID爲1的方法,上面的代碼返回我下面的完整陣列:`
{ id: 1,
children:[{
id: 1111
},
{
id: 2222
}
{
id: 3333
}]
}
但我只需要ID爲1111而不是競爭數組的數組特定元素。任何救世主都可以告訴我我在這裏做錯了什麼。 TIA :)
可能duplidate http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an -object-array-in-mongodb-collection – Ben
@Ben我已經通過鏈接,它不適合我,它返回我完整的數組集,如上所述。 –