id of a new item created via upsert

1,021 views
Skip to first unread message

Tim Oxley

unread,
Mar 20, 2012, 1:20:32 AM3/20/12
to mongoo...@googlegroups.com
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

Thomas Blobaum

unread,
Mar 20, 2012, 1:39:32 AM3/20/12
to mongoo...@googlegroups.com
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)."



Thomas Blobaum
https://github.com/tblobaum



On Tue, Mar 20, 2012 at 12:20 AM, Tim Oxley <sec...@gmail.com> 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 mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en

Tim Oxley

unread,
Mar 20, 2012, 6:34:13 AM3/20/12
to mongoo...@googlegroups.com
Hey Thomas, I believe you're right but I think this is an unsafe operation because there's no guarantee that the last command run was actually the one we just called due to node's async nature.

mongoose's safe mode is supposed to auto call getlasterror after each command automatically (this needs to be called by the driver to ensure it's called immediately after), but I think it's not surfacing all the info it gets back.

 

Aaron Heckmann

unread,
Mar 20, 2012, 10:12:34 AM3/20/12
to mongoo...@googlegroups.com


On Tue, Mar 20, 2012 at 6:34 AM, Tim Oxley <sec...@gmail.com> wrote:
mongoose's safe mode is supposed to auto call getlasterror after each command automatically (this needs to be called by the driver to ensure it's called immediately after), but I think it's not surfacing all the info it gets back.

Mongoose passes all info back. At this time the driver does not return any details from the getLastError cmd but the number of affected documents when doing an update w/ upsert. Open a ticket over on the driver here: https://github.com/christkv/node-mongodb-native/issues/new


--
Aaron



herbert yeung

unread,
Mar 24, 2013, 1:00:56 AM3/24/13
to mongoo...@googlegroups.com
Hi Aaron,

Just wondering whether you can tell an upsert did an insert or an update in the callback?
This appears to have been updated in the driver: https://github.com/mongodb/node-mongodb-native/issues/569

However, it does not appear I can check for this 'updatedExisting' value?!?

Thanks for your help,
Herb

Aaron Heckmann

unread,
Mar 25, 2013, 4:39:42 PM3/25/13
to mongoo...@googlegroups.com
Yes its available as the third argument passed to the callback:

YourModel.update(something, doc, function (err, numAffected, details) {
  console.log(details)
})



 
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 mongoo...@googlegroups.com
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Aaron


Reply all
Reply to author
Forward
0 new messages