
- 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
Remove and update the existing record in MongoDB?
You can use only $pull operator that removes and updates the existing record in MongoDB. Let us first create a collection with documents −
> db.removeDemo.insertOne( ... { ... "UserName" : "Larry", ... "UserDetails" : [ ... { ... "_id" : 101, ... "UserEmailId" : "976Larry@gmail.com", ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f9f88f9e6ff3eb0ce446") } > db.removeDemo.insertOne( ... { ... "UserName" : "Mike", ... "UserDetails" : [ ... { ... "_id" : 102, ... "UserEmailId" : "Mike121@gmail.com", ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f9f98f9e6ff3eb0ce447") }
Following is the query to display all documents from a collection with the help of find() method −
> db.removeDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc7f9f88f9e6ff3eb0ce446"), "UserName" : "Larry", "UserDetails" : [ { "_id" : 101, "UserEmailId" : "976Larry@gmail.com" } ] } { "_id" : ObjectId("5cc7f9f98f9e6ff3eb0ce447"), "UserName" : "Mike", "UserDetails" : [ { "_id" : 102, "UserEmailId" : "Mike121@gmail.com" } ] }
Let us now implement the $pull query to remove and update the existing record −
> db.removeDemo.update( ... {"_id": ObjectId("5cc7f9f98f9e6ff3eb0ce447")}, ... { "$pull": { "UserDetails": {"_id": 102}}} ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us display the documents from the collection in order to check the objects have been removed or not −
> db.removeDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc7f9f88f9e6ff3eb0ce446"), "UserName" : "Larry", "UserDetails" : [ { "_id" : 101, "UserEmailId" : "976Larry@gmail.com" } ] } { "_id" : ObjectId("5cc7f9f98f9e6ff3eb0ce447"), "UserName" : "Mike", "UserDetails" : [ ] }
- Related Articles
- How to update record in MongoDB without replacing the existing fields?
- How can we update a record in MongoDB?
- How do I add a field to existing record in MongoDB?
- How to remove duplicate record in MongoDB 3.x?
- How to update a MongoDB document without overwriting the existing one?
- How to update an existing document in MongoDB collection using Java?
- How to update or modify the existing documents of a collection in MongoDB?
- How do I remove ON UPDATE CURRENT_TIMESTAMP from an existing column in MySQL?
- Update existing column data in MySQL and remove the last string from a varchar column with strings and numbers
- How to remove a record from an existing table in oracle database using JDBC API?
- How to remove a record from an existing table in a database using JDBC API?
- Select last record and update it in MySQL?
- Update in MongoDB and prevent overwrite?
- Perform Group in MongoDB and sum the price record of documents
- Add, Update, and Remove Git Submodule

Advertisements