
- 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
Can MongoDB return result of increment?
Yes, you can achieve this with findAndModify(). Let us first create a collection with documents −
> db.returnResultOfIncementDemo.insertOne({"PlayerScore":98}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3c292edc6604c74817cda") }
Following is the query to display all documents from a collection with the help of find() method −
> db.returnResultOfIncementDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd3c292edc6604c74817cda"), "PlayerScore" : 98 }
Following is the query to return result of increment. Here, we have incremented PlayerScore by 2 −
> db.returnResultOfIncementDemo.findAndModify({ ... query:{}, ... update: { $inc: {PlayerScore: 2 }}, ... new: true ... });
This will produce the following output −
{ "_id" : ObjectId("5cd3c292edc6604c74817cda"), "PlayerScore" : 100 }
- Related Articles
- Can we return Result sets in JDBC?
- Can we remove _id from MongoDB query result?
- MongoDB Increment value inside nested array?
- How can I update and increment two fields in one command in MongoDB?
- How to increment a field in MongoDB?
- Increment MongoDB value inside a nested array
- MongoDB Aggregate group multiple result?
- Increment a value in a MongoDB nested object?
- Increment only a single value in MongoDB document?
- Auto increment in MongoDB to store sequence of Unique User ID?
- Increment value of an array element with array object in MongoDB
- Particular field as result in MongoDB?
- Alternative of MongoDB operator $eq to get similar result
- MongoDB projection result as an array of selected items?
- How to increment inside the update() function in MongoDB?

Advertisements