MongoDB equivalent for an SQL query

Question:

Any idea how to represent the following SQL condition for MongoDB

WHERE
     a = 1
AND  b = 2
AND  (c >= 3 OR c IS NULL)
AND  d = 4

Tried this, but seems not working:

{ a:1, b:2, c:{ $in:[ { $gte:3 }, { $exists: false } ] } , d:4 }

This doesn’t work since the key ‘c’ gets overridden:

{ a:1, b:3, $or:[ { c:{ $gte:3 } }, { c:{ $exists:false } } ] , d:4 }

Any help is greatly appreciated

Asked By: user795243

||

Answers:

I think this is what you’re looking for:

{
“a”: 1,
“b”: 2,
“$or”: [
{
“c”: {
“$gte”: 3
}
},
{
“c”: {
“$exists”: false
}
}
],
“d”: 4
}

Answered By: SomethingOn
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.