Is there a way when querying a MongoDB database to test for fields that either:
This should cover all three of your cases:
db.FOO.find({BAR: null});
References:
You can verify from the Mongo shell:
> db.foo.drop();
true
> db.foo.insert({_id:1, bar:1, matches:'NO'});
> db.foo.insert({_id:2, matches:'YES'});
> db.foo.insert({_id:3, bar:null, matches:'YES'});
> db.foo.insert({_id:4, bar:[1,2,3], matches:'NO'});
> db.foo.insert({_id:5, bar:[1,2,null], matches:'YES'});
>
> db.foo.find({bar: null});
{ "_id" : 2, "matches" : "YES" }
{ "_id" : 3, "bar" : null, "matches" : "YES" }
{ "_id" : 5, "bar" : [ 1, 2, null ], "matches" : "YES" }
> db.foo.count({bar: null});
3
> db.foo.count({matches: 'YES'});
3
$exists operator to check if a field has been set or not.
To check if the value of the field is null
, you can directly write a find query.
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