
- 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 find and modify a value in a nested array?
To find and modify a value in a nested array, you can use update command. Let us first create a collection with documents
> db.findAndModifyAValueInNestedArrayDemo.insertOne( { "CompanyName" : "Amazon", "DeveloperDetails" : [ { "ProjectName" : "Online Book Store", "TeamSize" : "5" }, { "ProjectName" : "Library Management System", "TeamSize" : "7" }, { "ProjectName" : "Online Banking Application", "TeamSize" : "15" } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5ca275226304881c5ce84b9f") }
Following is the query to display all documents from a collection with the help of find() method
> db.findAndModifyAValueInNestedArrayDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca275226304881c5ce84b9f"), "CompanyName" : "Amazon", "DeveloperDetails" : [ { "ProjectName" : "Online Book Store", "TeamSize" : "5" }, { "ProjectName" : "Library Management System", "TeamSize" : "7" }, { "ProjectName" : "Online Banking Application", "TeamSize" : "15" } ] }
Following is the query to find and modify a value in a nested array. We are modifying the TeamSize to 20 for ProjectName: "Online Banking Application"
> db.findAndModifyAValueInNestedArrayDemo.update( { DeveloperDetails: { $elemMatch: { ProjectName: "Online Banking Application" } } }, { $set: { 'DeveloperDetails.$.TeamSize': '20' } } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the document is modified with value 20 or not
> db.findAndModifyAValueInNestedArrayDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca275226304881c5ce84b9f"), "CompanyName" : "Amazon", "DeveloperDetails" : [ { "ProjectName" : "Online Book Store", "TeamSize" : "5" }, { "ProjectName" : "Library Management System", "TeamSize" : "7" }, { "ProjectName" : "Online Banking Application", "TeamSize" : "20" } ] }
- Related Articles
- How to modify properties of a nested object in JavaScript?
- How to modify an array value of a JSON object in JavaScript?
- Increment MongoDB value inside a nested array
- Accessing and returning nested array value - JavaScript?
- How to modify column default value in MySQL?
- How to sum all elements in a nested array? JavaScript
- How to get a value from a nested list in Rest Assured?
- How to group nested fields in MongoDB aggregation with count value in array?
- How can I search a collection to find a nested value in one of its documents in MongoDB?
- MongoDB Increment value inside nested array?
- How to modify a 2d Scatterplot to display color based on a third array in a CSV file?
- Increment a value in a MongoDB nested object?
- How to access and modify pixel value in an image using OpenCV Python?
- How to modify a const variable in C?
- How to remove an element from a doubly-nested array in a MongoDB document?

Advertisements