PHP driver getting ID back from upsert

196 views
Skip to first unread message

chris.chen21

unread,
Feb 27, 2012, 5:21:42 PM2/27/12
to mongodb-user
It seems mongoids are only returned for inserts when doing an update/
upsert command. I've been trying to get it to return an id
consistently regardless of an insert or update so I am using
findAndModify.

The only problem now is findAndModify doesn't seem to return whether
or not it updated an existing or inserted a new document.

My current hack involves returning the timestamp of the update and
comparing it to the creation date in the MongoId and seeing if it's
less than or equal to see if it was n update or insert (is this
safe?).

Is there a way to return the _id of the document that was inserted or
updated AND whether or not it was an update or insert?

Thanks!

Raxit Sheth

unread,
Feb 27, 2012, 5:58:08 PM2/27/12
to mongod...@googlegroups.com
getlasterror giving any usable stuff ?

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>

chris.chen21

unread,
Feb 27, 2012, 6:31:03 PM2/27/12
to mongodb-user
This is what it returns when I call lastError() after doing a
findAndModify
array
  'n' => int 0
  'lastOp' => float 5.7139611784855E+18
  'connectionId' => int 12
  'err' => null
  'ok' => float 1

This is what happens when I use an update/upsert:
array
'updatedExisting' => boolean true
'n' => int 1
'lastOp' => float 5.7139609938019E+18
'connectionId' => int 8
'err' => null
'ok' => float 1

The findAndModify returns the id (via the find) but doesn't return
whether or not it updatedExisting. However the update/upsert returns
updatedExisting but does not return an _id if it is an update.
Unfortunately I need both values and I can't seem to get them without
the hack.

Raxit Sheth

unread,
Feb 27, 2012, 7:01:51 PM2/27/12
to mongod...@googlegroups.com
when result is updatedexisting, does it really change _id ? can you check it ?

chris.chen21

unread,
Feb 27, 2012, 8:57:16 PM2/27/12
to mongodb-user
What do you mean?

When it's an update/upsert I put everything in $set and no it doesn't
add an _id field to the original array.

Glenn Maynard

unread,
Mar 1, 2012, 12:51:42 PM3/1/12
to mongod...@googlegroups.com
I've hit the same problem: upsert tells you the _id when it creates a new item (the "upserted" field from getLastError), but not if it updates an existing one.  If only one item is modified, the _id of the affected item should be returned.

(Even better would be a way to atomically return the modified document or a subset, as if a findOne(_id) was performed atomically with the update.)

--
Glenn Maynard


Kristina Chodorow

unread,
Mar 1, 2012, 3:18:06 PM3/1/12
to mongod...@googlegroups.com
You might want to watch/vote for https://jira.mongodb.org/browse/SERVER-3920.
Reply all
Reply to author
Forward
0 new messages