Request ORing and ANDing mongodb

I am using the mongoDB database. in which I have a collection in the format below.

{ name : name1 , area: a , country : c}
{ name : name2 , area: b , country : c}
{ name : name3 , area: a1 , country : c1}
{ name : name4 , area: b1 , country : c1}

I need a query like

select * from coll where (country =c and area =a) or (country = c1 and area = b1)

in mongodb request.

I read a lot of documents, but did not find a suitable answer.

So, if anyone knows, answer.

thank

+3
source share
1 answer

By default, all elements in a mongodb request use a statement and. You can specify the operator orusing the construct { "$or": [ ... ] }as described in the documentation .

Your request will look like this:

{ "$or": [ { "country": "c", "area": "a" }, { "country": "c1", "area": "b1" } ] }
+6
source

Source: https://habr.com/ru/post/1792236/


All Articles