
- 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 property value of an element in array object with MongoDB
To increment a property value of an element, use update() in MongoDB and in that, work with #$inc to increment. Let us first create a collection with documents −
> db.demo97.insertOne({ ... "Details": [ ... { ... "Name": "Chris", ... "Marks": 45 ... }, ... { ... "Name": "Bob", ... "Marks": 88 ... }y ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2d6d24b8903cdd865577af") }
Display all documents from a collection with the help of find() method −
> db.demo97.find();
This will produce the following output −
{ "_id" : ObjectId("5e2d6d24b8903cdd865577af"), "Details" : [ { "Name" : "Chris", "Marks" : 45 }, { "Name" : "Bob", "Marks" : 88 } ] }
Following is the query to increment a property value of an element in array object −
> db.demo97.update( ... { "Details.Name": "Bob"}, ... { $inc: { "Details.$.Marks" : 10 } } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo97.find();
This will produce the following output −
{ "_id" : ObjectId("5e2d6d24b8903cdd865577af"), "Details" : [ { "Name" : "Chris", "Marks" : 45 }, { "Name" : "Bob", "Marks" : 98 } ] }
- Related Articles
- Increment value of an array element with array object in MongoDB
- Increment a value in a MongoDB nested object?
- Increment MongoDB value inside a nested array
- MongoDB Increment value inside nested array?
- Find the number of times a value of an object property occurs in an array with JavaScript?
- Search a value in an object with number keys with MongoDB
- Querying from part of object in an array with MongoDB
- Increment only a single value in MongoDB document?
- Retrieve only the queried element in an object array in MongoDB collection?
- Replace an array field value with MongoDB?
- MongoDB query to insert an array element with a condition?
- Query on the last object of an array with MongoDB
- MongoDB query check if value in array property?
- Sorting an array object by property having falsy value - JavaScript
- MongoDB query to find property of first element of array?

Advertisements