mongodb - Mongo returning an array element -
i have following json document in mongodb added mingoimport.
i trying return single element questions array thequestion equals "q1".
{ "questions": [ { "questionentry": { "id": 1, "info": { "seasonnumber": 1, "episodenumber": 1, "episodename": "days gone bye" }, "questionitem": { "thequestion": "q1", "attachedelement": { "type": 1, "value": "" } }, "options": [ { "type": 1, "value": "o1" }, { "type": 1, "value": "o1" } ], "answer": { "questionid": 1, "answer": 1 }, "metatags": [ "season 1", "episode 1", "rick grimmes" ] } }, { "questionentry": { "id": 1, "info": { "seasonnumber": 1, "episodenumber": 1, "episodename": "days gone bye" }, "questionitem": { "thequestion": "q2", "attachedelement": { "type": 1, "value": "" } }, "options": [ { "type": 1, "value": "o2" }, { "type": 1, "value": "o2" } ], "answer": { "questionid": 1, "answer": 1 }, "metatags": [ "season 1", "episode 1", "rick grimmes", "glenn rhee" ] } } ] } i ran query db.questions.find({"questions.questionentry.questionitem.thequestion" : "q1"}) retruned whole document (both questionentry's in question array!
i have tried db.questions.find({"questions.questionentry.questionitem.thequestion" : "q1"}, _id:0," questions.questionitem": {$elemmatch : {thequestion: "q1"}}})
but following error: error: error: { "$err" : "can't canonicalize query: badvalue cannot use $elemmatch projection on nested field.", "code" : 17287
is there way limit result array element contains it?
thanks
db.questions.find({},{"questions.questionentry.questionitem.thequestion" : "q1"}); or
db.questions.find({"questions.questionentry.questionitem.thequestion" : "q1"},{'questions.$':1}); please try these.
Comments
Post a Comment