
- 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 value of an array element with array object in MongoDB
To increment the value of an array object, use $inc. Let us create a collection with documents −
>db.demo506.insertOne({"details":[{id:1,Quantity:4},{id:2,Quantity:3},{id:3,Quantity:2},{id:4,Qua ntity:7}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e882ed6987b6e0e9d18f576") }
Display all documents from a collection with the help of find() method −
> db.demo506.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e882ed6987b6e0e9d18f576"), "details" : [ { "id" : 1, "Quantity" : 4 }, { "id" : 2, "Quantity" : 3 }, { "id" : 3, "Quantity" : 2 }, { "id" : 4, "Quantity" : 7 } ] }
Following is the query to increment the value of an array element name quantity with array object −
> db.demo506.update({"details.id":2},{$inc:{"details.$.Quantity":10}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo506.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e882ed6987b6e0e9d18f576"), "details" : [ { "id" : 1, "Quantity" : 4 }, { "id" : 2, "Quantity" : 13 }, { "id" : 3, "Quantity" : 2 }, { "id" : 4, "Quantity" : 7 } ] }
- Related Articles
- Increment a property value of an element in array object with MongoDB
- MongoDB Increment value inside nested array?
- Increment MongoDB value inside a nested array
- Querying from part of object in an array with MongoDB
- Replace an array field value with MongoDB?
- Increment a value in a MongoDB nested object?
- Retrieve only the queried element in an object array in MongoDB collection?
- Query on the last object of an array with MongoDB
- MongoDB query to insert an array element with a condition?
- MongoDB query to access an object in an array
- Pull an element in sub of sub-array in MongoDB?
- MongoDB query for Partial Object in an array
- Match element in array of MongoDB?
- Display only an element found in an array in MongoDB?
- Maximum value in an array after m range increment operations in C++

Advertisements