mongodbmongodb-scala

mongodb ORing and ANDing query


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

{ 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 want query like

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

in mongodb query.

I have read many document but not found suitable answer.

So if any one knows please reply.

Thanks


Solution

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

    Your query will be :

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