
- 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
How to increment inside the update() function in MongoDB?
You can use $inc operator to increment. Let us first create a collection with documents −
> db.addInUpdateFunctionDemo.insertOne({"PlayerName":"Chris","PlayerScore":78}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2b3f4345990cee87fd893") } > db.addInUpdateFunctionDemo.insertOne({"PlayerName":"Robert","PlayerScore":88}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2b3fc345990cee87fd894") } > db.addInUpdateFunctionDemo.insertOne({"PlayerName":"David","PlayerScore":99}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2b407345990cee87fd895") }
Following is the query to display all documents from a collection with the help of find() method −
> db.addInUpdateFunctionDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2b3f4345990cee87fd893"), "PlayerName" : "Chris", "PlayerScore" : 78 } { "_id" : ObjectId("5cd2b3fc345990cee87fd894"), "PlayerName" : "Robert", "PlayerScore" : 88 } { "_id" : ObjectId("5cd2b407345990cee87fd895"), "PlayerName" : "David", "PlayerScore" : 99 }
Following is the query to perform addition inside the update() function in MongDB −
> db.addInUpdateFunctionDemo.update({_id : ObjectId("5cd2b407345990cee87fd895")},{$inc: {PlayerScore: 201}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check all the documents once again −
> db.addInUpdateFunctionDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2b3f4345990cee87fd893"), "PlayerName" : "Chris", "PlayerScore" : 78 } { "_id" : ObjectId("5cd2b3fc345990cee87fd894"), "PlayerName" : "Robert", "PlayerScore" : 88 } { "_id" : ObjectId("5cd2b407345990cee87fd895"), "PlayerName" : "David", "PlayerScore" : 300 }
- Related Articles
- MongoDB Increment value inside nested array?
- Increment MongoDB value inside a nested array
- Update elements inside an array in MongoDB?
- How to perform Increment in MySQL Update?
- How can I update and increment two fields in one command in MongoDB?
- How to increment a field in MongoDB?
- How to update MongoDB Object?
- How to update all documents in MongoDB?
- How to update multiple documents in MongoDB?
- How to update _id field in MongoDB?
- How to update after aggregate in MongoDB?
- How to do conditional update in MongoDB?
- How to update array of subdocuments in MongoDB?
- How to update only one property in MongoDB?
- How to update MongoDB collection using $toLower?

Advertisements