mongodb - How can I provide multiple criteria for an attribute within an element of array in mongo query? -


i have collection following documents:


    {        "_id": 1,          "books": [            {              "id":"sherlock holmes",              "category":"novel"            },            {              "id":"10 tips cook",              "category":"tips"            }          ]       },       {          "_id": 2,          "books": [              {            "id":"10 tips cook",              "category":"tips"          }           ]       },       {        "_id": 3,          "books": [            {              "id":"sherlock holmes",              "category":"novel"            }          ]       } 

i want query document contains both books id "sherlock holmes" , "10 tips cook", "_id" 1.

i've tried $in , $elemmatch results three. need 1 in case.

do have solutions?

use $and operator search same field multiple expression.

db.coll.find({     '$and': [         {'books.id': 'sherlock holmes'},         {'books.id': '10 tips cook'}     ] }) 

result:

{     "_id" : 1,     "books" : [          {             "id" : "sherlock holmes",             "category" : "novel"         },          {             "id" : "10 tips cook",             "category" : "tips"         }     ] } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -