![]() |
VOOZH | about |
Querying null and missing fields in MongoDB (including MongoDB Atlas) helps handle incomplete data accurately using operators like $eq, $exists, $ne, and $or.
{ "_id": 1, "name": "Alen", "email": "alen@example.com", "phone": "123-456-7890" }
{ "_id": 2, "name": "Ben", "email": null, "phone": "458-654-3210" }
{ "_id": 3, "name": "Clevin", "email": "clevin@example.com", "phone": null }
{ "_id": 4, "name": "Denial", "email": null, "phone": null }
To query for documents where a specific field is null, MongoDB provides the $eq operator. This operator matches documents where the specified field is equal to the specified value, including null.
We have a collection named students with documents representing user profiles. We want to find users who have not provided their email addresses.
Query:
db.students.find({ email: { $eq: null } })Output:
{ _id: 2, name: 'Ben', email: null, phone: 545 }
{ _id: 4, name: 'Denial', email: null, phone: null }
This query returns documents where the email field is null or missing.
Use the $exists operator with false to return documents where a field is missing.
To find students that do not have a description field.
Query:
db.students.find({ description: { $exists: false } })Output:
{ _id: 1, name: 'Alen', email: "alen@example.com", phone: "123-456-7890" }
{ _id: 2, name: 'Ben', email: null, phone: 458-654-3210 }
{ _id: 3, name: 'Clevin', email: "clevin@example.com", phone: null }
{ _id: 4, name: 'Denial', email: null, phone: null }
In some cases, we may need to combine conditions to query for documents with null or missing fields based on multiple criteria. MongoDB allows us to use logical operators like $and, $or, and $not for this purpose.
We want to find students who have not provided their email addresses or phone numbers:
Query:
db.students.find({
$or: [
{ email: { $exists: false } },
{ email: null },
{ phone: { $exists: false } },
{ phone: null }
]
})
Output:
{ _id: 2, name: 'Ben', email: null, phone: '458-654-3210'}
{_id: 3, name: 'Clevin', email: 'c@gmail.com', phone: null}
{_id: 4, name: 'Denial ', email: null, phone: null }
This query uses $or to match documents where email or phone is missing or explicitly null.