
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 update a MongoDB document without overwriting the existing one?
- How to remove duplicate record in MongoDB 3.x?
- How to update or modify the existing documents of a collection in MongoDB?
- Select last record and update it in MySQL?
- How to update an existing document in MongoDB collection using Java?
- Update existing column data in MySQL and remove the last string from a varchar column with strings and numbers
- How do I remove ON UPDATE CURRENT_TIMESTAMP from an existing column in MySQL?
- How to remove a record from an existing table in a database using JDBC API?
- How to remove a record from an existing table in oracle database using JDBC API?
- Update in MongoDB and prevent overwrite?
- Perform Group in MongoDB and sum the price record of documents
- Update a column in MySQL and remove the trailing underscore values
Advertisements