
- 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 field in MongoDB document which is embedded?
Let’s say, here we are incrementing StudentScores for MongoDB which is inside StudentDetails −
... "StudentScores": { ... "StudentMathScore": 90, ... "StudentMongoDBScore": 78 ... }
Let us first create a collection with documents −
> db.embeddedValueIncrementDemo.insertOne( ... { ... "StudentDetails": { ... "StudentScores": { ... "StudentMathScore": 90, ... "StudentMongoDBScore": 78 ... } ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2b670345990cee87fd896") }
Following is the query to display all documents from a collection with the help of find() method −
> db.embeddedValueIncrementDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2b670345990cee87fd896"), "StudentDetails" : { "StudentScores" : { "StudentMathScore" : 90, "StudentMongoDBScore" : 78 } } }
Following is the query to increment embedded value. Here, we are incrementing StudentMongoDBScore −
> db.embeddedValueIncrementDemo.update({ _id: new ObjectId("5cd2b670345990cee87fd896") }, { $inc: { "StudentDetails.StudentScores.StudentMongoDBScore": 20 } }, { upsert: true, safe: true }, null); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check all the documents once again −
> db.embeddedValueIncrementDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2b670345990cee87fd896"), "StudentDetails" : { "StudentScores" : { "StudentMathScore" : 90, "StudentMongoDBScore" : 98 } } }
- Related Articles
- Add a field to an embedded document in an array in MongoDB?
- Return specific MongoDB embedded document
- MongoDB query for fields in embedded document?
- Implement MongoDB $push in embedded document array?
- Increment only a single value in MongoDB document?
- How to increment a field in MongoDB?
- How to get embedded data in a MongoDB document?
- MongoDB query to return only embedded document?
- How to find a certain element in the MongoDB embedded document?
- Filter query on array of embedded document with MongoDB?
- Retrieving an embedded object as a document via the aggregation framework in MongoDB?
- MongoDB - How to check for equality in collection and in embedded document?
- Add new field to every document in a MongoDB collection?
- In MongoDB how do you use $set to update a nested value/embedded document?
- Check if value exists for a field in a MongoDB document?

Advertisements