I have the following documents in the mongoDB
{ "_id" : ObjectId("5215c622f8b2bae39d82b54c"), "params" : { "groupBy" : "days", "country" : "A", "city" : "b" } }
{ "_id" : ObjectId("5215c62df8b2bae39d82b54d"), "params" : { "groupBy" : "days", "country" : "A" } }
{ "_id" : ObjectId("5215c643f8b2bae39d82b54e"), "params" : { "groupBy" : "days" } }
and I tried to find the first document with this query
{params : {groupBy : 'days', country : 'A', 'city' : 'b'}}
OK, no problem, it works. Let's try this
{params : {groupBy : 'days', city : 'b', country : 'A'}}
and this one do not work surprisingly. OK so now I know order does matter.
I know I can do this to search the first document if I want to ignore the order of attributes.
{ 'params.groupBy' : 'days' , 'params.city' : 'b', 'params.country' : 'A' }
Now consider I want to find the 2nd document and I don't know the order of the attributes before. So what I will do is
{'params.groupBy' : 'days' , 'params.country' : 'A'}
but this query find both the 1st and the 2nd documents.
So how can I find the 2nd document with only one query?
Edit: One more condition is I am not sure how many attributes will have in the params sub-document in the future and it may keep changing from time to time
{params:{groupBy:"days",country:"A"}}{params:{groupBy:"days",country:"A"}}or{params:{country:"A", groupBy:"days"}}