Im trying to get specific quiz in database by id:
static getQuiz(db, id, cb){ //db is database connection, id is quiz id, cb is just callback
db.find({ _id : "8RA4Rey50eqKFlWK"}, {"quiz" : { $elemMatch : { _id : id}}}, function(err, Doc){
if(cb){
cb(err,Doc);
console.log(Doc); //only return _id : "8RA4Rey50eqKFlWK"
}
})
}
JSON Database:
{
"_id":"8RA4Rey50eqKFlWK",
"quiz":[
{
"_id":"1b944055-2b15-4838-7e7a-beef4c9a5a62",
"title":"test",
"description":""
},
{
"_id":"7dc53529-206c-6003-1d3c-133264d7ad81",
"title":"aaaa",
"description":""
},
{
"_id":"db3c788f-56b3-f9c8-8a25-affb2981e12f",
"title":"lala",
"description":""
},
{
"_id":"20388c1f-1a00-4f7b-3d25-9db56247a6bf",
"title":"asdasd",
"description":""
}
]
}
above code not working, already tested with:
db.find({ _id : "8RA4Rey50eqKFlWK"}, {"quiz" : { $elemMatch : { title : "test"}}})
but result still just root id => _id : "8RA4Rey50eqKFlWK".
i expects result when search quiz by id "1b944055-2b15-4838-7e7a-beef4c9a5a62" should be:
{
"_id":"8RA4Rey50eqKFlWK",
"quiz":[
{
"_id":"1b944055-2b15-4838-7e7a-beef4c9a5a62",
"title":"test",
"description":""
},
]
}
Any solutions? im actually using NeDB https://github.com/louischatriot/nedb which is same syntax as MongoDB. thanks
EDIT: i tried my code using mongodb console, its works beautifully! maybe thats just NeDB bug?