Peter Lucas
Peter Lucas

Reputation: 1991

Combine $and and $or conditions in a MongoDB query where document has embedded documents

I have a collection with many documents;

enter image description here

I would like to return all documents where individuals are (older than 30 AND less than 37 years old), OR live in Italy.

My attempt

db.getCollection('persons')
                          .find(                                
                                    $or:[
                                            {$and: [
                                                        {age:{$gte:30}},
                                                        {age:{$lt:37}}
                                                   ],
                                            {"company.location.country": "Italy"}
                                       ]
                                )

Which yields an error

enter image description here

Upvotes: 0

Views: 47

Answers (1)

Daniel Khoroshko
Daniel Khoroshko

Reputation: 2721

It can be simplified to

{
  $or: [
    { age: { $lt: 37, $gte: 30 } },
    { "company.localtion.country": "Italy" }
  ]
}

Upvotes: 1

Related Questions