
- 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 can I delete an item from an Object in MongoDB?
To delete an item from an object in MongoDB, use $unset. Let us create a collection with documents −
> db.demo467.insertOne( ... { ... _id:101, ... "Information":{"Name":"Chris"} ... } ... ); { "acknowledged" : true, "insertedId" : 101 } > db.demo467.insertOne( ... { ... _id:102, ... "Information":{"Name":"David"} ... } ... ); { "acknowledged" : true, "insertedId" : 102 }
Display all documents from a collection with the help of find() method −
> db.demo467.find();
This will produce the following output −
{ "_id" : 101, "Information" : { "Name" : "Chris" } } { "_id" : 102, "Information" : { "Name" : "David" } }
Following is the query to delete an item from an Object −
> db.demo467.update({_id:102},{$unset: {"Information.Name":1}},{multi: true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo467.find();
This will produce the following output −
{ "_id" : 101, "Information" : { "Name" : "Chris" } } { "_id" : 102, "Information" : { } }
- Related Articles
- How can I remove a specific item from an array in JavaScript
- How can I remove a specific item from an array JavaScript?
- How to insert an item to an array that is inside an object in MongoDB?
- Can I get the first item in a Cursor object in MongoDB?
- How to delete element from an array in MongoDB?
- How can I delete an element in Selenium using Python?
- how can I declare an Object Array in Java?
- In PHP, how can I add an object element to an array?
- How to get items from an object array in MongoDB?
- How to remove an item from an ArrayList in C#?
- How to remove an item from an ArrayList in Kotlin?
- How do I delete array value from a document in MongoDB?
- How to Delete Item from Hashtable Collection in C#?
- How can I convert an array to an object by splitting strings? JavaScript
- MongoDB query to find data from an array inside an object?

Advertisements