
- 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
In MongoDB how do you use $set to update a nested value/embedded document?
The syntax is as follows for this −
db.yourCollectionName.update({ }, { $set: { "yourOuterFieldName.yourInnerFieldName": "yourValue" } });
To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.updateNestedValueDemo.insertOne({"CustomerName":"Chris", ... "CustomerDetails":{"CustomerAge":25,"CustomerCompanyName":"Google","CustomerCityName":"US"}}); { "acknowledged" : true, "insertedId" : ObjectId("5c8fccc4d3c9d04998abf015") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.updateNestedValueDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8fccc4d3c9d04998abf015"), "CustomerName" : "Chris", "CustomerDetails" : { "CustomerAge" : 25, "CustomerCompanyName" : "Google", "CustomerCityName" : "US" } }
Here is the query to use $set to update a nested value/embedded document −
> db.updateNestedValueDemo.update({ }, { $set: { "CustomerDetails.CustomerCompanyName": "Dell" } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the documents from a collection using find() method −
> db.updateNestedValueDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8fccc4d3c9d04998abf015"), "CustomerName" : "Chris", "CustomerDetails" : { "CustomerAge" : 25, "CustomerCompanyName" : "Dell", "CustomerCityName" : "US" } }
Look at the above sample output, the nested field “CustomerCompanyName” has been changed from “Google” to “Dell”.
- Related Articles
- MongoDB query to update nested document
- How do you update a MongoDB document while replacing the entire document?
- MongoDB query to update the nested document?
- Set condition in MongoDB nested document?
- Perform nested document value search in MongoDB?
- How to get embedded data in a MongoDB document?
- Update only a specific value in a MongoDB document
- Updating Nested Embedded Documents in MongoDB?
- How to find a certain element in the MongoDB embedded document?
- How to update document with marks value in MongoDB for student David
- Return specific MongoDB embedded document
- MongoDB query to return only embedded document?
- Updating nested document in MongoDB
- MongoDB query for fields in embedded document?
- Implement MongoDB $push in embedded document array?

Advertisements