update bug?

7 views
Skip to first unread message

mwaschkowski

unread,
Aug 25, 2010, 3:24:17 PM8/25/10
to mongodb-user
Hi,

Using:
-http://fastdl.mongodb.org/win32/mongodb-win32-x86_64-1.6.1.zip
-java 2.1 driver (mongo-2.1.jar)

When I do:

1 //db.getCollection(collectionName).update(finder, dbObject,
true, false);
2 db.getCollection(collectionName).insert(dbObject);
3 System.out.println(dbObject.get("_id"));

I see an ID come through, but if I comment out line 2 and uncomment
line 1, I get null. I start from an empty collection in both cases.

Fill out a jira?

Thanks,

Mark

Eliot Horowitz

unread,
Aug 25, 2010, 3:30:10 PM8/25/10
to mongod...@googlegroups.com
You'll have to send a full example - there are cases where that is the
correct behavior.
You can open a jira with a full example if you want.

> --
> 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.
>
>

mwaschkowski

unread,
Aug 25, 2010, 4:20:38 PM8/25/10
to mongodb-user
OK. When would be the case when "_id" isn't populated? I thought mongo
would always add a "_id" to a persisted object if one didn't exist
already?

Eliot Horowitz

unread,
Aug 25, 2010, 4:22:58 PM8/25/10
to mongod...@googlegroups.com
The server does.
The client only does on insert.

mwaschkowski

unread,
Aug 25, 2010, 4:41:56 PM8/25/10
to mongodb-user
Can you be more specific?

For me:

1 collection.insert --> adds "_id"
2 collection.update --> does not add "_id" when a brand new object
'updated' with insert=true ie. upserted

I'm assuming that in the 2nd case is not correct and have entered a
jira for it.

Scott Hernandez

unread,
Aug 25, 2010, 4:42:00 PM8/25/10
to mongod...@googlegroups.com
The only time this could work for an update is in the case of an upsert (insert done by the server) and only through some client-side work in the driver, and with a server v1.5.4+.

Please create an issue in jira if you want this feature.

Eliot Horowitz

unread,
Aug 25, 2010, 4:43:21 PM8/25/10
to mongod...@googlegroups.com
1) yes - the client adds it
2) the server adds in this case, not the client. so you can't access
in the method you spcified

mwaschkowski

unread,
Aug 25, 2010, 4:56:30 PM8/25/10
to mongodb-user
Thank you Scott and Eliot for bringing this (what turned out to be)
trivial issue to resolution. I did not understand the difference
between the two cases (client does this, server does that) so it
looked very strange to me. I believe that the system should behave the
same via either method of storing an object in the system, and that in
the meantime the docs are updated to reflect the difference (ie. to
make a note that "_id" not populated when upserting).

I created an improvement item:

http://jira.mongodb.org/browse/SERVER-1686

I would be happy to update the javadocs for the java driver to reflect
the different behaviors, is there a way for me to do so?

Thanks guys,

Mark

Eliot Horowitz

unread,
Aug 25, 2010, 4:59:47 PM8/25/10
to mongod...@googlegroups.com
This is not a change that will ever happen.
We cannot add an _id client side since we don't know what's going to
happen on the server.

You can get the _id from an update by doing getLastError

> db.getLastErrorCmd()
{
"err" : null,
"updatedExisting" : false,
"upserted" : ObjectId("4c7584283d83d07c78674f34"),
"n" : 1,
"ok" : 1
}


You can update the javadocs by forking and submitting a patch.

Scott Hernandez

unread,
Aug 25, 2010, 5:01:43 PM8/25/10
to mongod...@googlegroups.com
Well, there is one important thing that could complicate this that we haven't talked about. There is no guarantee that you specify a document on the update, you could specify a set of operations ($set/$push/etc). In that case setting the _id value on the update operations makes less sense, and you still have a (possibly) confusing situation.

To make updates to the java driver go to github, fork the repo (into your account), make changes and send a pull request. It is that simple. It is also documented on the website in how to contribute.

Scott Hernandez

unread,
Aug 25, 2010, 5:05:16 PM8/25/10
to mongod...@googlegroups.com
Eliot, in the case where the WriteConcern has already caused the GLE call to be made we could set the _id on the document, but it seems like a lot of work for a (very) special case.

mwaschkowski

unread,
Aug 25, 2010, 5:09:23 PM8/25/10
to mongodb-user
Oh, OK, not a problem. Scott did mention making a feature request, so
always happy to comply ;)

Will attempt do the patch to the docs tomorrow, haven't used Git, but
wanted to anyway...

mwaschkowski

unread,
Aug 25, 2010, 5:11:43 PM8/25/10
to mongodb-user
Interesting, thanks for the follow up Scott.

If its a lot of work, I think a comment in the update method javadoc
would be better, I'm sure you guys have more than enough to do :)

Best,

Mark

On Aug 25, 5:05 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> Eliot, in the case where the WriteConcern has already caused the GLE call to
> be made we could set the _id on the document, but it seems like a lot of
> work for a (very) special case.
>
> On Wed, Aug 25, 2010 at 1:59 PM, Eliot Horowitz <eliothorow...@gmail.com>wrote:
>
>
>
> > This is not a change that will ever happen.
> > We cannot add an _id client side since we don't know what's going to
> > happen on the server.
>
> > You can get the _id from an update by doing getLastError
>
> > > db.getLastErrorCmd()
> > {
> >        "err" : null,
> >        "updatedExisting" : false,
> >        "upserted" : ObjectId("4c7584283d83d07c78674f34"),
> >        "n" : 1,
> >        "ok" : 1
> > }
>
> > You can update the javadocs by forking and submitting a patch.
>
> > On Wed, Aug 25, 2010 at 4:56 PM, mwaschkowski <mwaschkow...@gmail.com>
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsubscribe@google groups.com>
> > .
> > >> >> >> > For more options, visit this group athttp://
> > groups.google.com/group/mongodb-user?hl=en.
>
> > >> >> > --
> > >> >> > 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<mongodb-user%2Bunsubscribe@google groups.com>
> > .
> > >> >> > For more options, visit this group athttp://
> > groups.google.com/group/mongodb-user?hl=en.
>
> > >> > --
> > >> > 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<mongodb-user%2Bunsubscribe@google groups.com>
> > .
> > >> > For more options, visit this group athttp://
> > groups.google.com/group/mongodb-user?hl=en.
>
> > > --
> > > 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<mongodb-user%2Bunsubscribe@google groups.com>
> > .
> > > For more options, visit this group at
> >http://groups.google.com/group/mongodb-user?hl=en.
>
> > --
> > 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<mongodb-user%2Bunsubscribe@google groups.com>
> > .

Eliot Horowitz

unread,
Aug 25, 2010, 5:09:46 PM8/25/10
to mongod...@googlegroups.com
That's a bit scary to me because the update behavior changes based on
WriteConcern, which seems a bit confusing to me.
Or at least very likely to cause confusion/mayhem when debugging

Scott Hernandez

unread,
Aug 25, 2010, 5:17:09 PM8/25/10
to mongod...@googlegroups.com
On Wed, Aug 25, 2010 at 2:09 PM, Eliot Horowitz <elioth...@gmail.com> wrote:
That's a bit scary to me because the update behavior changes based on
WriteConcern, which seems a bit confusing to me.
Or at least very likely to cause confusion/mayhem when debugging

Yep, it seems the best behavior is to document that need to call the WriteResult.getLastError() if you need the new _id after the update (assuming it was an upsert).
Reply all
Reply to author
Forward
0 new messages