1

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?

tonywei
  • 677
  • 2
  • 12
  • 25
  • I tried your code now in mongo2.6.9 and returns the expected result. – Moi Syme Apr 07 '17 at 18:39
  • Yes that looks like a functionality not provided in nedb. Have you tried positional variant ? `db.find( { _id : "8RA4Rey50eqKFlWK", "quiz" : { $elemMatch : { title : "test" } } }, { "quiz.$" : 1 } )` or `db.find( { _id : "8RA4Rey50eqKFlWK", "quiz.title" : "test" }, { "quiz.$" : 1 } )` – s7vr Apr 07 '17 at 19:46
  • It looks like the issue is that you only want to return a specific element of the "quiz" array. $elemMatch is used to match based on elements of the array but will still return the entire document. See my answer below about using the aggregation framework to restructure the document. – Adam Harrison Apr 07 '17 at 21:45

3 Answers3

0
        db.a.find(     
          {_id : "8RA4Rey50eqKFlWK"},
          { 
             "quiz" : { 
                  $elemMatch : { "title" : "test"}
                     }
          });

miss spelled title to _title

Vinayk93
  • 353
  • 1
  • 6
0

You can try this:

db.collection.find( { 
        _id: "8RA4Rey50eqKFlWK",
        "quiz" : { 
           $elemMatch : { "title" : "test"}
              }} );
Moi Syme
  • 466
  • 2
  • 8
0

If I understand correctly, you want a query to return the document in included in your message (the one with "_id":"8RA4Rey50eqKFlWK") and only display the quiz with "_id":"1b944055-2b15-4838-7e7a-beef4c9a5a62", is this correct?

If so, you'll need to use the aggregation framework.

The following aggregation framework query will query for the document by _id, unwind the "quiz" array and filter out the specific sub-document in the array:

db.quiz.aggregate([ 
{ "$match" : { _id: "8RA4Rey50eqKFlWK" } }, 
{ "$unwind" : "$quiz" },
{ "$match" : { "quiz._id" : "1b944055-2b15-4838-7e7a-beef4c9a5a62" } }
] )

Returns:

{ "_id" : "8RA4Rey50eqKFlWK", "quiz" : { "_id" : "1b944055-2b15-4838-7e7a-beef4c9a5a62", "title" : "test", "description" : "" } }

I provided a detailed breakdown of the aggregation framework in another one of my answers.

Community
  • 1
  • 1
Adam Harrison
  • 3,323
  • 2
  • 17
  • 25