
- 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
Increment MongoDB value inside a nested array
To increment a value inside a nested array, use positional operator ($). Let us first create a collection with documents
> db.incrementInNestedArrayDemo.insertOne( ... { ... "StudentId":100, ... "ProjectDetails": ... [ ... {"ProjectId":567778888, ... "TeamSize":4 ... }, ... { ... "ProjectId":67888999, ... "TeamSize":2 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9a6b7b15e86fd1496b38aa") }
Following is the query to display all documents from a collection with the help of find() method
> db.incrementInNestedArrayDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9a6b7b15e86fd1496b38aa"), "StudentId" : 100, "ProjectDetails" : [ { "ProjectId" : 567778888, "TeamSize" : 4 }, { "ProjectId" : 67888999, "TeamSize" : 2 } ] }
Following is the query to increment value inside nested array
> db.incrementInNestedArrayDemo.update({ "StudentId" : 100, "ProjectDetails.ProjectId":67888999} , {$inc:{"ProjectDetails.$.TeamSize":1}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check that the field TeamSize is incremented by 1 or not. Following is the query
> db.incrementInNestedArrayDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9a6b7b15e86fd1496b38aa"), "StudentId" : 100, "ProjectDetails" : [ { "ProjectId" : 567778888, "TeamSize" : 4 }, { "ProjectId" : 67888999, "TeamSize" : 3 } ] }
- Related Articles
- MongoDB Increment value inside nested array?
- Increment a value in a MongoDB nested object?
- Increment value of an array element with array object in MongoDB
- Increment a property value of an element in array object with MongoDB
- How to increment inside the update() function in MongoDB?
- Increment only a single value in MongoDB document?
- Group objects inside the nested array JavaScript
- Clearing items in a nested MongoDB array?
- MongoDB $push in nested array?
- Write a MongoDB query to get nested value?
- Get array items inside a MongoDB document?
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- MongoDB query to increment a specific value using custom variable
- MongoDB Aggregation to slice array inside array

Advertisements