I've the following data in my collection:
{ colour: { r: 0, g: 0, b: 0 }},
{ colour: null },
How can I find all the documents that have colour == null
or color.r
between some values?
I've tried
.find({ where: { $or: [{colour: null}, {"colour.r": {$gt: 0, $lt: 100}}]}})
but of course this gives me cannot read property 'r' of null
for null rows.
Use Object. Object. keys will return an array, which contains the property names of the object. If the length of the array is 0 , then we know that the object is empty.
In a nutshell, the Optional class includes methods to explicitly deal with the cases where a value is present or absent. However, the advantage compared to null references is that the Optional class forces you to think about the case when the value is not present.
A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.
Use $where
only if there is no other way to express your query
db.test.find({$or: [{"colour": null}, {"colour.r": {$gt: 0, $lt: 100}}]})
For sake of completeness:
db.test.find({$nor: [{"colour.r": {$lte: 0}}, {"colour.r": {$gte: 100}}]})
$nor
will match all documents that fail the expressions.
Here, you don't have to explicitly test for null
as it is neither greater nor lower than any number -- hence, will fail the tests, just like any number in the range (0,100)1
1Exclusive. If you need to find every document in the range [0,100] inclusive, replace $gte
(resp. $lte
) by $gt
(resp. $lt
).
Unless don't need a single query, you could run 2 queries:
where color == null
where color != null and color between 0 and 100
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With