Mansa Mansa - 1 year ago 89
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 Source

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.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download