FAQ
You can get the new _id by running an additional command
`db.runCommand('getlasterror')` and parsing what you get back, or you could
also use findAndModify with {upsert: true} but it only updates one doc.
"The client may determine if its most recent message on a connection
updated an existing object by subsequently issuing a getlasterror
command ( db.runCommand(
"getlasterror" ) ). If the result of the getlasterror command contains an
updatedExisting field, the last message on the connection was an update
request. If the updatedExisting field's value is true, that update request
caused an existing object to be updated; if updatedExisting is false, no
existing object was updated. An "upserted" field will contain the new _id value
if an insert is performed (new as of 1.5.4)."

http://www.mongodb.org/display/DOCS/Updating#Updating-CheckingtheOutcomeofanUpdateRequest
http://www.mongodb.org/display/DOCS/findAndModify+Command


Thomas Blobaum
https://github.com/tblobaum


On Tue, Mar 20, 2012 at 12:20 AM, Tim Oxley wrote:

How can I get the id of a freshly upserted item? Currently update with
{upsert: true} gives me no indicator other than how many items were
updated. The info should be available when in {safe: true} mode, somewhere
according to "Checking the Outcome of an Update Request" in
http://www.mongodb.org/display/DOCS/Updating but I'm not sure if perhaps
I'm missing something.

Thanks

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoose-orm@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en
--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoose-orm@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 6 | next ›
Discussion Overview
groupmongoose-orm @
categoriesmongodb, nodejs
postedMar 20, '12 at 5:20a
activeMar 25, '13 at 9:10p
posts6
users4
websitemongoosejs.com
irc#mongoosejs

People

Translate

site design / logo © 2022 Grokbase