Need some MongoDB help, query operator

Hello all,

Need some MongoDB related help here.

Let’s say I have a collection with document’s field of “status”.

The possible “status” value (single string) is:

  • “empty”
  • “full”
  • “quarter”
  • “half”

Now in query, if I need to find document with “status” value of either “quarter” or “half”.

Is there any operator in MongoDB that is suitable for this query? Where I can supply an array of keywords to query a string field.

I read the documentation, seems like don’t have one that meet my needs. Maybe I missed it.

Do I have to query twice in separate query? Please advice, thank you.

Regards

Try a Mongo $or:

MyCollection.find({$or: [{status: 'quarter'},{status: 'half'}]})

Or, in this case, since it’s the same key, a Mongo $in:

MyCollection.find( { status: { $in: ['quarter', 'half'] }} )

Will try this when I get back to workstation. Thanks so much for your advice.