How to check empty field in a MongoDB collection?

To check empty field in a MongoDB collection, use $exists along with $eq operator. The $exists ensures the field is present, while $eq matches the empty string value.

Syntax

db.collection.find({
    "fieldName": {
        "$exists": true,
        "$eq": ""
    }
});

Create Sample Data

Let us create a collection with documents containing empty and non-empty fields ?

db.demo485.insertMany([
    {"FirstName": "Chris", "LastName": ""},
    {"FirstName": "David", "LastName": "Miller"},
    {"FirstName": "Chris", "LastName": "Brown"},
    {"FirstName": "Robert", "LastName": ""}
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5e82e9f6b0f3fa88e22790a8"),
        ObjectId("5e82e9fdb0f3fa88e22790a9"),
        ObjectId("5e82ea03b0f3fa88e22790aa"),
        ObjectId("5e82ea0fb0f3fa88e22790ab")
    ]
}

Display All Documents

View all documents in the collection ?

db.demo485.find();
{ "_id": ObjectId("5e82e9f6b0f3fa88e22790a8"), "FirstName": "Chris", "LastName": "" }
{ "_id": ObjectId("5e82e9fdb0f3fa88e22790a9"), "FirstName": "David", "LastName": "Miller" }
{ "_id": ObjectId("5e82ea03b0f3fa88e22790aa"), "FirstName": "Chris", "LastName": "Brown" }
{ "_id": ObjectId("5e82ea0fb0f3fa88e22790ab"), "FirstName": "Robert", "LastName": "" }

Query for Empty Fields

Find all documents where the LastName field exists but is empty ?

db.demo485.find({
    "LastName": {
        "$exists": true,
        "$eq": ""
    }
});
{ "_id": ObjectId("5e82e9f6b0f3fa88e22790a8"), "FirstName": "Chris", "LastName": "" }
{ "_id": ObjectId("5e82ea0fb0f3fa88e22790ab"), "FirstName": "Robert", "LastName": "" }

Key Points

  • $exists: true ensures the field is present in the document
  • $eq: "" matches fields that contain an empty string
  • This approach distinguishes between missing fields and empty string values

Conclusion

Use $exists with $eq to identify documents with empty string fields. This combination ensures you find fields that exist but contain no data, excluding documents where the field is completely missing.

Updated on: 2026-03-15T03:08:22+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements