javascript - MongoDB $elemMatch not working for subdocument -
im trying specific quiz in database id:
static getquiz(db, id, cb){ //db database connection, id quiz id, cb 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, tested with:
db.find({ _id : "8ra4rey50eqkflwk"}, {"quiz" : { $elemmatch : { title : "test"}}})
but result still root id => _id : "8ra4rey50eqkflwk".
i expects result when search quiz id "1b944055-2b15-4838-7e7a-beef4c9a5a62" should be:
{ "_id":"8ra4rey50eqkflwk", "quiz":[ { "_id":"1b944055-2b15-4838-7e7a-beef4c9a5a62", "title":"test", "description":"" }, ] }
any solutions? im using nedb https://github.com/louischatriot/nedb same syntax mongodb. thanks
edit: tried code using mongodb console, works beautifully! maybe thats nedb bug?
db.a.find( {_id : "8ra4rey50eqkflwk"}, { "quiz" : { $elemmatch : { "title" : "test"} } });
miss spelled title _title
Comments
Post a Comment