
- 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
MongoDB query to update a specific document from a collection
To update, use $set along with UPDATE. Let us create a collection with documents −
>db.demo135.insertOne({"Details":[{"EmployeeId":101,"EmployeeName":"Chris","EmployeeSalary":45000},{"EmployeeId":102,"EmployeeName":"Chris","EmployeeSalary":45000}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31a5ddfdf09dd6d085399c") }
Display all documents from a collection with the help of find() method −
> db.demo135.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e31a5ddfdf09dd6d085399c"), "Details" : [ { "EmployeeId" : 101, "EmployeeName" : "Chris", "EmployeeSalary" : 45000 }, { "EmployeeId" : 102, "EmployeeName" : "Chris", "EmployeeSalary" : 45000 } ] }
Following is the query to update second document −
> db.demo135.update( ... { ... ... "Details.EmployeeId":102, ... ... }, ... { ... $set: { ... "Details.$.EmployeeName" : "John Doe" ... } ... } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo135.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e31a5ddfdf09dd6d085399c"), "Details" : [ { "EmployeeId" : 101, "EmployeeName" : "Chris", "EmployeeSalary" : 45000 }, { "EmployeeId" : 102, "EmployeeName" : "John Doe", "EmployeeSalary" : 45000 } ] }
- Related Articles
- MongoDB query to pull a specific value from a document
- MongoDB query to remove a specific document
- MongoDB query to find a specific city record from a collection
- MongoDB query to update nested document
- Update only a specific value in a MongoDB document
- MongoDB query to update the nested document?
- Accessing array values in a MongoDB collection to fetch a specific document
- MongoDB query select and display only a specific field from the document?
- MongoDB query to add a document in an already created collection
- MongoDB query to implement nor query to fetch documents except a specific document
- Filter specific values from a MongoDB document
- How to update an existing document in MongoDB collection using Java?
- MongoDB query to remove array elements from a document?
- Get the top most document from a MongoDB collection
- How to run MongoDB query to update only a specific field value?

Advertisements