
- 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 a value in a MongoDB nested object?
To increment a value in nested object, you can use $inc operator. Let us first implement the following query to create a collection with documents
>db.incrementValueDemo.insertOne({"StudentName":"Larry","StudentCountryName":"US","StudentDetails":[{"StudentSubjectName":"Math","StudentMathMarks":79}]}); { "acknowledged" : true, "insertedId" : ObjectId("5c986ca0330fd0aa0d2fe4a2") }
Following is the query to display all documents from a collection with the help of find() method
> db.incrementValueDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c986ca0330fd0aa0d2fe4a2"), "StudentName" : "Larry", "StudentCountryName" : "US", "StudentDetails" : [ { "StudentSubjectName" : "Math", "StudentMathMarks" : 79 } ] }
Following is the query to increment a value in nested object. The marks would get incremented here
> db.incrementValueDemo.update( {"StudentDetails.StudentSubjectName":"Math"}, { $inc : { "StudentDetails.$.StudentMathMarks" : 1 } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Following is the query to check the value is incremented or not
> db.incrementValueDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c986ca0330fd0aa0d2fe4a2"), "StudentName" : "Larry", "StudentCountryName" : "US", "StudentDetails" : [ { "StudentSubjectName" : "Math", "StudentMathMarks" : 80 } ] }
- Related Articles
- Increment MongoDB value inside a nested array
- MongoDB Increment value inside nested array?
- Increment a property value of an element in array object with MongoDB
- Increment only a single value in MongoDB document?
- How to retrieve a nested object in MongoDB?
- Increment value of an array element with array object in MongoDB
- Write a MongoDB query to get nested value?
- Extract key value from a nested object in JavaScript?
- MongoDB query to increment a specific value using custom variable
- Group query upon nested object in MongoDB?
- MongoDB $addToSet to add a deep nested array of object?
- Perform nested document value search in MongoDB?
- How to increment a field in MongoDB?
- Changing value of nested object keys in JavaScript
- Clearing items in a nested MongoDB array?

Advertisements