
- 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 unset objects in MongoDB?
To unset objects in MongoDB, use $unset. Let us create a collection with documents −
> db.demo348.insertOne( ... { ... "details": { ... "studentDetails": ... { ... StudentName: "Robert" ... } ... } ... ... }); { "acknowledged" : true, "insertedId" : ObjectId("5e553556f8647eb59e5620b2") } > db.demo348.insertOne( ... { ... "details": { ... "studentDetails": ... { ... StudentName: "Mike" ... } ... } ... ... }); { "acknowledged" : true, "insertedId" : ObjectId("5e553563f8647eb59e5620b3") }
Display all documents from a collection with the help of find() method −
> db.demo348.find();
This will produce the following output &mius;
{ "_id" : ObjectId("5e553556f8647eb59e5620b2"), "details" : { "studentDetails" : { "StudentName" : "Robert" } } } { "_id" : ObjectId("5e553563f8647eb59e5620b3"), "details" : { "studentDetails" : { "StudentName" : "Mike" } } }
Following is the query to unset objects −
> db.demo348.update({},{$unset: { "details.studentDetails": ""}},{multi: true}); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Display all documents from a collection with the help of find() method −
> db.demo348.find();
This will produce the following output −
{ "_id" : ObjectId("5e553556f8647eb59e5620b2"), "details" : { } } { "_id" : ObjectId("5e553563f8647eb59e5620b3"), "details" : { } }
- Related Articles
- How to unset a variable in MongoDB shell?
- MongoDB query to $pull / $unset with multiple conditions?
- Unset an attribute from a single array element in MongoDB?
- How to unset a JavaScript variable?
- How can I update child objects in MongoDB?
- How to find all objects created before specified Date in MongoDB?
- How to query objects with the longest time period in MongoDB?
- How to compare attributes of different objects in MongoDB object array?
- Access objects from the nested objects structure in MongoDB
- How can I update child objects in MongoDB database?
- Query deeply nested Objects in MongoDB
- Find objects between two dates in MongoDB?
- MongoDB query to remove empty objects in an object-array?
- How to select objects where an array contains only a specific field in MongoDB?
- Find objects created in last week in MongoDB?

Advertisements