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
Answers:
I think this is what you’re looking for:
{
“a”: 1,
“b”: 2,
“$or”: [
{
“c”: {
“$gte”: 3
}
},
{
“c”: {
“$exists”: false
}
}
],
“d”: 4
}
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
I think this is what you’re looking for:
{
“a”: 1,
“b”: 2,
“$or”: [
{
“c”: {
“$gte”: 3
}
},
{
“c”: {
“$exists”: false
}
}
],
“d”: 4
}