Reputation: 6076
Is is possible to do a MongoDB increment ($inc) and return the result of the increment as part of the same call, rather than having to make two calls?
Upvotes: 3
Views: 554
Reputation: 3752
You will have to use findandModify().
From the docs:
Modifies and returns a single document. By default, the returned document does not include the modifications made on the update. To return the document with the modifications made on the update, use the new option. The findAndModify() method is a shell helper around the findAndModify command.
You can use as such:
db.yourCollection.findAndModify({
query:{},
update: { $inc: { propertyToIncrement: 1 }},
new: true
})
Upvotes: 5