
- 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 Increment value inside nested array?
You can use positional operator $ for this. To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.incrementValueInNestedArrayDemo.insertOne( ... {"UniqueId":1, ... "StudentDetails": ... [ ... { ... "StudentId":101, ... "StudentMarks":97 ... }, ... { ... "StudentId":103, ... "StudentMarks":99 ... }, ... { ... "StudentId":105, ... "StudentMarks":69 ... }, ... { ... "StudentId":107, ... "StudentMarks":59 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c77dd71fc4e719b197a12f7") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.incrementValueInNestedArrayDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c77dd71fc4e719b197a12f7"), "UniqueId" : 1, "StudentDetails" : [ { "StudentId" : 101, "StudentMarks" : 97 }, { "StudentId" : 103, "StudentMarks" : 99 }, { "StudentId" : 105, "StudentMarks" : 92 }, { "StudentId" : 107, "StudentMarks" : 59 } ] }
Here is the query to increment a value in the nested array −
> db.incrementValueInNestedArrayDemo.update({UniqueId:1,"StudentDetails.StudentId":107}, {$inc:{"StudentDetails.$.StudentMarks":1}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check nested array value is incremented or not in the above collection with StudentId 107 −
> db.incrementValueInNestedArrayDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c77dd71fc4e719b197a12f7"), "UniqueId" : 1, "StudentDetails" : [ { "StudentId" : 101, "StudentMarks" : 97 }, { "StudentId" : 103, "StudentMarks" : 99 }, { "StudentId" : 105, "StudentMarks" : 92 }, { "StudentId" : 107, "StudentMarks" : 60 } ] }
Look at the sample output, the field name “StudentMarks” has been updated from 59 to 60 that means increments by 1.
- Related Articles
- Increment MongoDB value inside a nested array
- Increment a value in a MongoDB nested object?
- Increment value of an array element with array object in MongoDB
- How to increment inside the update() function in MongoDB?
- Increment a property value of an element in array object with MongoDB
- MongoDB $push in nested array?
- Group objects inside the nested array JavaScript
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- Increment only a single value in MongoDB document?
- MongoDB Aggregation to slice array inside array
- How to group nested fields in MongoDB aggregation with count value in array?
- Perform nested document value search in MongoDB?
- Clearing items in a nested MongoDB array?
- Query array of nested string with MongoDB?

Advertisements