
- 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
Removing empty fields from MongoDB
To remove empty fields, use deleteMany(). Let us first create a collection with documents −
> db.removeEmptyFieldsDemo.insertOne({"StudentName":""}); { "acknowledged" : true, "insertedId" : ObjectId("5ce92b9578f00858fb12e919") } > db.removeEmptyFieldsDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce92b9878f00858fb12e91a") } > db.removeEmptyFieldsDemo.insertOne({"StudentName":""}); { "acknowledged" : true, "insertedId" : ObjectId("5ce92b9c78f00858fb12e91b") } > db.removeEmptyFieldsDemo.insertOne({"StudentName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce92ba078f00858fb12e91c") }
Following is the query to display all documents from a collection with the help of find() method −
> db.removeEmptyFieldsDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5ce92b9578f00858fb12e919"), "StudentName" : "" } { "_id" : ObjectId("5ce92b9878f00858fb12e91a"), "StudentName" : "Chris" } { "_id" : ObjectId("5ce92b9c78f00858fb12e91b"), "StudentName" : "" } { "_id" : ObjectId("5ce92ba078f00858fb12e91c"), "StudentName" : "Robert" }
Following is the query to remove empty fields from MongoDB −
> db.removeEmptyFieldsDemo.updateMany({"StudentName": ""}, { $unset : {"StudentName" : 1 }}); { "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
Let us check the document once again −
> db.removeEmptyFieldsDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5ce92b9578f00858fb12e919") } { "_id" : ObjectId("5ce92b9878f00858fb12e91a"), "StudentName" : "Chris" } { "_id" : ObjectId("5ce92b9c78f00858fb12e91b") } { "_id" : ObjectId("5ce92ba078f00858fb12e91c"), "StudentName" : "Robert" }
- Related Articles
- Removing item from array in MongoDB?
- Removing all the empty indices from array in JavaScript
- Retrieving a Subset of Fields from MongoDB
- Removing an array element from a MongoDB collection
- Removing empty array elements in PHP
- How to retrieve all nested fields from MongoDB collection?
- MongoDB query to return specific fields from an array?
- Concatenate fields in MongoDB?
- Removing duplicates and inserting empty strings in JavaScript
- Removing an array element from MongoDB collection using update() and $pull
- Concatenate strings from two fields into a third field in MongoDB?
- MySQL group_concat to add a separator for empty fields?
- How to query a document in MongoDB comparing fields from an array?
- Get fields from multiple sub-documents that match a condition in MongoDB?
- Update only specific fields in MongoDB?

Advertisements