我有我的集合中的下列JSON文档:MongoDB的嵌套查询结构
{
"_id": {
"$oid": "59e3a593734d1d62dcbe79c3"
}
"name": "UserNameKiran"
"notebooks": [
{
"notebookname": "notebook1",
"notes": [
{
"name": "noteName"
},
{
"name": "noteName2"
}
]
},
{
"access": "public",
"notebookname": "notebook2",
"notes": [
{
"name": "noteName"
},
{
"name": "noteName2"
}
]
}
]
};
我想检索所有来自特定用户和笔记本电脑“注意事项”。例如:“notebook1”的所有注释。
我试过下面的命令,但无法得到任何结果。
req.db.collection('usernotecollection').find({
"_id": ObjectId(req.params.userId),
"notebooks": {
"$elemMatch": {
"notebookname": "notebook1"
}
}
}, {notes:1}).toArray(function (err, results) {
console.log(results);
});
它只是返回给我的object_id而没有其他的结果。我错过了什么?
感谢您的明确解释。但是,我无法获得特定笔记本的笔记。出于某种原因,“notebookname”:“notebook1”的匹配没有发生。 –
Tehre在名为'notebookname'的'notebooks'子文件中没有属性,也许你的意思是'notes.name'。一般来说,这种形式的问题(如何从这个文档中获得x?)最好用(1)样本文件; (2)你尝试过的查询的例子和(3)**所需的输出**。 – glytching
谢谢我能解决我的问题 –