
- 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
MongoDB query to remove empty objects in an object-array?
You can use $pull operator for this. Let us first create a collection with documents. Here, we have also added an empty object −
> db.removeEmptyObjectsDemo.insertOne( { "_id" :101, "LoginDate" :new ISODate(), "UserDetails" : [ { "UserName" : "John" }, { }, { "UserName" : "Sam" } ] } ); { "acknowledged" : true, "insertedId" : 101 }
Following is the query to display all documents from a collection with the help of find() method −
> db.removeEmptyObjectsDemo.find().pretty();
This will produce the following output −
{ "_id" : 101, "LoginDate" : ISODate("2019-05-25T04:46:29.505Z"), "UserDetails" : [ { "UserName" : "John" }, { }, { "UserName" : "Sam" } ] }
Following is the query to remove empty objects in an object-array −
> db.removeEmptyObjectsDemo.update( {}, { "$pull": { "UserDetails": { "UserName": { "$exists": false } } } }, { "multi": true } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the documents from the above collection −
> db.removeEmptyObjectsDemo.find().pretty();
This will produce the following output. The empty object removed successfully −
{ "_id" : 101, "LoginDate" : ISODate("2019-05-25T04:46:29.505Z"), "UserDetails" : [ { "UserName" : "John" }, { "UserName" : "Sam" } ] }
- Related Articles
- MongoDB query to access an object in an array
- MongoDB query for Partial Object in an array
- MongoDB query to match and remove element from an array?
- MongoDB query to remove item from array?
- MongoDB query to find data from an array inside an object?
- Remove object from array in MongoDB?
- How to remove object from array in MongoDB?
- MongoDB query to remove entire array from collection?
- MongoDB query to update array object in index N?
- MongoDB query to remove array elements from a document?
- Query on the last object of an array with MongoDB
- MongoDB query to find multiple matchings inside array of objects?
- MongoDB query to remove element from array as sub property
- MongoDB query to replace value in an array?
- How to compare attributes of different objects in MongoDB object array?

Advertisements