
- 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 update or modify the existing documents of a collection in MongoDB?
To update or modify the existing documents of a collection in MongoDB, you need to use update() method. The syntax is as follows:
db.yourCollectionName.update(yourExistingValue, yourUpdatedValue);
Here, we will create a collection with name updateinformation. The query to create a collection is as follows. MongoDB creates a collection automatically when you insert some document using insert() method as shown below:
> db.updateInformation.insert({"StudentName":"Larry",StudentAge:35,StudentMarks:89});
The following is the output:
WriteResult({ "nInserted" : 1 })
Now you can display the documents with the help of find() method from the collection updateinformation. The query is as follows:
> db.updateInformation.find();
The following is the output displaying the documents in the collection we added above:
{ "_id" : ObjectId("5c6aa29a64f3d70fcc9147f7"), "StudentName" : "Larry", "StudentAge" : 35, "StudentMarks" : 89 }
Now, let us update or modify the existing documents ‘StudentAge’ 35 to 24. For this, we will use the update() method. The query is as follows:
> db.updateInformation.update({StudentAge:35},{$set:{StudentAge:24}});
The following is the output:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
We have updated the StudentAge 35 to 24 above. Let us check the document once again. The query is as follows:
> db.updateInformation.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6aa29a64f3d70fcc9147f7"), "StudentName" : "Larry", "StudentAge" : 24, "StudentMarks" : 89 }
Look at the StudentAge field above. The age is now updated to 24/ Previously, it was 35.
- Related Articles
- How to Update multiple documents in a MongoDB collection using Java?
- How to update an existing document in MongoDB collection using Java?
- MongoDB query to update each field of documents in collection with a formula?
- How to validate documents before insert or update in MongoDB?
- How to count the number of documents in a MongoDB collection?
- How to update all documents in MongoDB?
- How to update multiple documents in MongoDB?
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- How to sort the documents of a MongoDB collection using java?
- How to return documents of a collection without objectId in MongoDB?
- Check for existing documents/embedded documents in MongoDB
- How to delete all the documents from a collection in MongoDB?
- Limit the number of documents in a collection in MongoDB?
- How to find two random documents in a MongoDB collection of 6?
