
- 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 Array element in MongoDB?
Use $addToSet operator to update array element. Let us first create a collection with documents −
> db.updateArrayDemo.insertOne( ... { ... ... "ClientDetails" : [ ... { ... "ClientName" : "John", ... "DeveloperDetails" : [ ] ... }, ... { ... "ClientName" : "Larry", ... "DeveloperDetails" : [ ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ccf465edceb9a92e6aa1960") }
Following is the query to display all documents from a collection with the help of find() method −
> db.updateArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ccf465edceb9a92e6aa1960"), "ClientDetails" : [ { "ClientName" : "John", "DeveloperDetails" : [ ] }, { "ClientName" : "Larry", "DeveloperDetails" : [ ] } ] }
Following is the query to update array element −
> db.updateArrayDemo.update({ "ClientDetails.ClientName": "Larry" }, { $addToSet: { "ClientDetails.$.Technology": { 'DeveloperName': "Chris", 'WorkExperience':5 } } }, false, true); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the array element has been updated or not −
> db.updateArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ccf465edceb9a92e6aa1960"), "ClientDetails" : [ { "ClientName" : "John", "DeveloperDetails" : [ ] }, { "ClientName" : "Larry", "DeveloperDetails" : [ ], "Technology" : [ { "DeveloperName" : "Chris", "WorkExperience" : 5 } ] } ] }
- Related Articles
- Update field in exact element array in MongoDB?
- Update an array element matching a condition using $push in MongoDB
- Removing an array element from MongoDB collection using update() and $pull
- MongoDB query to update an array element matching a condition using $push?
- Update elements inside an array in MongoDB?
- Update multiple elements in an array in MongoDB?
- Update object at specific Array Index in MongoDB?
- How to update array of subdocuments in MongoDB?
- Update array in MongoDB document by variable index?
- How to update array with multiple conditions in MongoDB
- Update objects in a MongoDB documents array (nested updating)?
- MongoDB query to update array object in index N?
- Match element in array of MongoDB?
- Update object in array with a specific key in MongoDB
- MongoDB query to update array with another field?

Advertisements