중첩 나는 내 컬렉션에 다음과 같은 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는 'notebooks' 하위 문서에'notebookname'이라는 속성이 없습니다. 아마도 notes.name을 의미 할 것입니다. 일반적으로이 양식의 질문 (_how는이 문서에서 x를 얻을 수 있습니까? _)은 (1) 견본 문서와 함께 가장 잘 질문됩니다. (2) 시도한 쿼리의 예제 및 (3) ** 원하는 출력 **. – glytching
감사합니다. 문제를 해결할 수있었습니다. –