So I'm attempting to find all records who have a field set and isn't null.
I try using $exists
, however according to the MongoDB documentation, this query will return fields who equal null.
$exists
does match documents that contain the field that stores the null value.
So I'm now assuming I'll have to do something like this:
db.collection.find({ "fieldToCheck" : { $exists : true, $not : null } })
Whenever I try this however, I get the error [invalid use of $not]
Anyone have an idea of how to query for this?
This question is related to
mongodb
Use $ne
(for "not equal")
db.collection.find({ "fieldToCheck": { $exists: true, $ne: null } })
Suppose we have a collection like below:
{
"_id":"1234"
"open":"Yes"
"things":{
"paper":1234
"bottle":"Available"
"bottle_count":40
}
}
We want to know if the bottle field is present or not?
Ans:
db.products.find({"things.bottle":{"$exists":true}})
db.<COLLECTION NAME>.find({ "<FIELD NAME>": { $exists: true, $ne: null } })
i find that this works for me
db.getCollection('collectionName').findOne({"fieldName" : {$ne: null}})
Source: Stackoverflow.com