Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to query for records where field is null or not set in MongoDB?
In MongoDB, you can query for records where a field is null or not set using different operators. There are two distinct cases: when a field exists but is set to null, and when a field doesn't exist in the document at all.
Syntax
// Field exists and is null
db.collection.find({"fieldName": null});
// Field does not exist
db.collection.find({"fieldName": {$exists: false}});
// Field is null OR does not exist
db.collection.find({$or: [{"fieldName": null}, {"fieldName": {$exists: false}}]});
Sample Data
db.fieldIsNullOrNotSetDemo.insertMany([
{"EmployeeName": "Larry", "EmployeeAge": null, "EmployeeSalary": 18500},
{"EmployeeName": "Bob", "EmployeeAge": 21, "EmployeeSalary": 23500},
{"EmployeeName": "Carol", "EmployeeSalary": 45500},
{"EmployeeName": "Mike", "EmployeeAge": null, "EmployeeSalary": 45500},
{"EmployeeName": "Ramit", "EmployeeSalary": 85500}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("..."),
ObjectId("..."),
ObjectId("..."),
ObjectId("..."),
ObjectId("...")
]
}
Display all documents to see the data structure ?
db.fieldIsNullOrNotSetDemo.find().pretty();
{
"_id": ObjectId("5c8a995c6cea1f28b7aa07fe"),
"EmployeeName": "Larry",
"EmployeeAge": null,
"EmployeeSalary": 18500
}
{
"_id": ObjectId("5c8a99836cea1f28b7aa07ff"),
"EmployeeName": "Bob",
"EmployeeAge": 21,
"EmployeeSalary": 23500
}
{
"_id": ObjectId("5c8a999b6cea1f28b7aa0800"),
"EmployeeName": "Carol",
"EmployeeSalary": 45500
}
{
"_id": ObjectId("5c8a99bb6cea1f28b7aa0801"),
"EmployeeName": "Mike",
"EmployeeAge": null,
"EmployeeSalary": 45500
}
{
"_id": ObjectId("5c8a99d76cea1f28b7aa0802"),
"EmployeeName": "Ramit",
"EmployeeSalary": 85500
}
Case 1: Field is Present but Set to Null
Count documents where EmployeeAge field exists but is set to null ?
db.fieldIsNullOrNotSetDemo.count({EmployeeAge: null});
4
Note: This returns 4 because it matches both null values (Larry, Mike) AND missing fields (Carol, Ramit). MongoDB treats missing fields as null when using this syntax.
Case 2: Field Does Not Exist
Count documents where EmployeeAge field is completely missing ?
db.fieldIsNullOrNotSetDemo.count({EmployeeAge: {$exists: false}});
2
Method 3: Strict Null Check (Recommended)
To find only documents where a field exists and is explicitly set to null ?
db.fieldIsNullOrNotSetDemo.find({
EmployeeAge: null,
EmployeeAge: {$exists: true}
});
Key Points
-
{field: null}matches both null values and missing fields -
{field: {$exists: false}}matches only missing fields -
{field: {$exists: true}}matches only existing fields (regardless of value) - Combine operators for precise matching of your specific use case
Conclusion
Use {field: null} for broad null/missing queries, {$exists: false} for strictly missing fields, and combine both operators when you need precise control over null vs missing field matching.
