Mansa Mansa - 5 months ago 22
JSON Question

Find with multiple condition like or

I am fairly new to MongoDB but am getting a hang of it, but... I can't seem to figure out how to do this:

I have a user which is a part of multiple groups. These groups posts news on there walls. Now I am trying to find all news from this users groups and then sort them after the newest ones.

Here is how I am trying to do this so far:

dataList.find({ "groupId": [groupIds], "tstamp": { $gt: tstamp }}).limit( 5 );


I think that the groupIds should be an json object like this:

{"12345", "54321"}


but that doesn't work :-/

Hope somebody can help me and thanks in advance :-)

Answer

You can use mongoDB $in clause for this task:

dataList.find({ "groupId": {$in : groupIds}, "tstamp": { $gt: tstamp }}).limit( 5 );

groupIds must be an array of ids.

See https://docs.mongodb.com/manual/reference/operator/query/in/

Comments