The { item : null } query matches documents that either contain the item field whose value is null or that do not contain the item field. The query returns both documents in the collection.
To query for is not null value, we can use the $ne operator as well as the $eq operator and specify the desired value that we want to query for. This guide aims to provide readers with different ways and examples for the same to query for is not null values in MongoDB.
Basically, we can use the $exists a method to implement the not null in MongoDB. When <boolean> is valid, $exists coordinates with the records that contain the field, including reports where the field esteem is invalid. In case <boolean> is bogus, the question returns just the records that don't contain the field.
If the sent_at
field is not there when its not set then:
db.emails.count({sent_at: {$exists: false}})
If it's there and null, or not there at all:
db.emails.count({sent_at: null})
If it's there and null:
db.emails.count({sent_at: { $type: 10 }})
The Query for Null or Missing Fields section of the MongoDB manual describes how to query for null and missing values.
Equality Filter
The
{ item : null }
query matches documents that either contain the item field whose value isnull
or that do not contain theitem
field.db.inventory.find( { item: null } )
Existence Check
The following example queries for documents that do not contain a field.
The
{ item : { $exists: false } }
query matches documents that do not contain theitem
field:db.inventory.find( { item : { $exists: false } } )
Type Check
The
{ item : { $type: 10 } }
query matches only documents that contain theitem
field whose value isnull
; i.e. the value of the item field is of BSON TypeNull
(type number10
) :db.inventory.find( { item : { $type: 10 } } )
If you want to ONLY count the documents with sent_at
defined with a value of null
(don't count the documents with sent_at
not set):
db.emails.count({sent_at: { $type: 10 }})
Use:
db.emails.count({sent_at: null})
Which counts all emails whose sent_at property is null or is not set. The above query is same as below.
db.emails.count($or: [
{sent_at: {$exists: false}},
{sent_at: null}
])
Seems you can just do single line:
{ "sent_at": null }
All the above answers are amazing and correct. Just want to add one improvement on the answers which is using $type
.
Starting with MongoDB 3.2, you can avoid using 10
(as hardcoded literals reduce the code readability) and instead can simply use string aliases i.e. "null"
. So to summarize-
sent_at
exists and has the value null
db.emails.count({sent_at: { $type: 'null' }});
// or
// This reduces the code readability as your peer might not know
// that what is the meaning of value 10
db.emails.count({sent_at: { $type: 10 }});
sent_at: null
or sent_at
does not exist// This will ensure both the conditions
db.emails.count({ sent_at: null })
sent_at
does not existdb.emails.count({sent_at: { $exists: false }});
sent_at
where the field exists and may have any valuedb.emails.count({sent_at: { $exists: true }});
Remember, this will pull any document of emails
which has any value including null
, 0
, ''
, false
.
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