
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to check empty field in a MongoDB collection?
To check empty field in a MongoDB collection, use $exists along with $eq operator. Let us create a collection with documents −
> db.demo485.insertOne({"FirstName":"Chris","LastName":""});{ "acknowledged" : true, "insertedId" : ObjectId("5e82e9f6b0f3fa88e22790a8") } > db.demo485.insertOne({"FirstName":"David","LastName":"Miller"});{ "acknowledged" : true, "insertedId" : ObjectId("5e82e9fdb0f3fa88e22790a9") } > db.demo485.insertOne({"FirstName":"Chris","LastName":"Brown"});{ "acknowledged" : true, "insertedId" : ObjectId("5e82ea03b0f3fa88e22790aa") } > db.demo485.insertOne({"FirstName":"Robert","LastName":""});{ "acknowledged" : true, "insertedId" : ObjectId("5e82ea0fb0f3fa88e22790ab") }
Display all documents from a collection with the help of find() method −
> db.demo485.find();
This will produce the following output −
{ "_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" : "" }
Following is the query to check empty field −
> db.demo485.find({"LastName" : {"$exists" : true, "$eq" : ""}})
This will produce the following output −
{ "_id" : ObjectId("5e82e9f6b0f3fa88e22790a8"), "FirstName" : "Chris", "LastName" : "" } { "_id" : ObjectId("5e82ea0fb0f3fa88e22790ab"), "FirstName" : "Robert", "LastName" : "" }
- Related Articles
- How to check if a Laravel collection is empty?
- How to update a single field in a capped collection in MongoDB?
- Project specific array field in a MongoDB collection?
- Add new field to every document in a MongoDB collection?
- How to add a new field to all the documents in a MongoDB collection
- How to check if field is a number in MongoDB?
- How to check if a field in MongoDB is [] or {}?
- How to check if field is null or empty in MySQL?
- Export specified field of a collection in mongodb / mongodump to file?
- How to check if a text field is empty or not in swift?
- C# Program to Check a HashTable collection is empty or not
- Add field that is unique index to collection in MongoDB?
- Check whether a field is empty or null in MySQL?
- Check if a list is not empty in MongoDB?
- MongoDB - How to check for equality in collection and in embedded document?

Advertisements