[SERVER-1092] update using $inc returns value incremented to Created: 05/May/10  Updated: 12/Jul/16  Resolved: 14/May/10

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Kenny Gorman Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

Currently we can do:

foo=db.kg.findAndModify({query:

{"dogs":2}

,update:{$inc:

{"dogs":1}

}})

{ "_id" : ObjectId("4bc665954c25a502cc9c76ef"), "dogs" : 2 }

foo

{ "_id" : ObjectId("4bc665954c25a502cc9c76ef"), "dogs" : 2 }

db.kg.find()

{ "_id" : ObjectId("4bc665954c25a502cc9c76ef"), "dogs" : 3 }

I think this would be nicer:

foo=db.kg.update({},{$inc:{"dogs":1}} )
foo

{"dogs":4}

and perhaps even:

foo=db.kg.update({},{$inc{"dogs":1}},{$inc{"cats":1}})
foo

{"dogs":5,"cats":2}

Returning the incremented value allows applications to use the newly generated value



 Comments   
Comment by Kenny Gorman [ 14/May/10 ]

You can close this. The above example is what we are looking for.

Comment by Kenny Gorman [ 14/May/10 ]

This works great...

al02 /home/kgorman$ mongo
MongoDB shell version: 1.5.1
url: test

> foo=db.kg.findAndModify({update:{$inc:{"dogs":1}},new:"True"},{update:{$inc:{"cats":1}},new:"True"})

{ "_id" : ObjectId("4beddb287ec0a997b44c7924"), "dogs" : 2, "cats" : 2 }

> foo

{ "_id" : ObjectId("4beddb287ec0a997b44c7924"), "dogs" : 2, "cats" : 2 }

>

Generated at Thu Feb 08 02:56:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.