
- 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
Update objects in a MongoDB documents array (nested updating)?
To update the objects in a document’s array, you need to use update() method. To understand the update() method, let us create a collection with document. The query to create a collection with document is as follows:
> db.updateObjects.insertOne({"CustomerId":1,"CustomerName":"Larry","TotalItems":100, ... "ItemDetails":[ ... { ... "NameOfItem":"Item_1", ... "Amount":450 ... }, ... { ... "NameOfItem":"Item_2", ... "Amount":500 ... }, ... { ... "NameOfItem":"Item_3", ... "Amount":200 ... } ... ] ... } ... );
The following is the output:
{ "acknowledged" : true, "insertedId" : ObjectId("5c6d688b0c3d5054b766a769") }
Now you can display documents from a collection with the help of find() method. The query is as follows:
> db.updateObjects.find().pretty();
The following is the output displaying the documents from the collection created above:
{ "_id" : ObjectId("5c6d688b0c3d5054b766a769"), "CustomerId" : 1, "CustomerName" : "Larry", "TotalItems" : 100, "ItemDetails" : [ { "NameOfItem" : "Item_1", "Amount" : 450 }, { "NameOfItem" : "Item_2", "Amount" : 500 }, { "NameOfItem" : "Item_3", "Amount" : 200 } ] }
Increment “Amount”:200 with value 130. The query is as follows. Here, we have used the $inc operator to increment the value of a field:
> db.updateObjects.update({"CustomerId":1,"ItemDetails.NameOfItem":"Item_3"}, {$inc:{"ItemDetails.$.Amount":130}}, ... false,true); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
We have successfully updated incremented the value. Let us display the document from the collection. The query is as follows:
> db.updateObjects.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6d688b0c3d5054b766a769"), "CustomerId" : 1, "CustomerName" : "Larry", "TotalItems" : 100, "ItemDetails" : [ { "NameOfItem" : "Item_1", "Amount" : 450 }, { "NameOfItem" : "Item_2", "Amount" : 500 }, { "NameOfItem" : "Item_3", "Amount" : 330 } ] }
Look at the sample output, the Amount 200 is incremented with 130, which is 330 now.
- Related Articles
- Updating Nested Embedded Documents in MongoDB?
- Update an array of strings nested within an array of objects in MongoDB
- Querying on an array of objects for specific nested documents with MongoDB?
- Updating nested document in MongoDB
- Find in MongoDB documents with filled nested array and reshape the documents result
- Aggregation in MongoDB for nested documents?
- MongoDB query to get only specific fields in nested array documents?
- Query deeply nested Objects in MongoDB
- Find MongoDB documents where all objects in array have specific value?
- How to update all documents in MongoDB?
- How to update multiple documents in MongoDB?
- MongoDB - update partial number of documents?
- MongoDB query to update nested document
- Access objects from the nested objects structure in MongoDB
- Fastest way of updating in MongoDB is update() or save()?
