Upsert on _id field with Java driver

121 views
Skip to first unread message

David the Dude

unread,
May 10, 2012, 1:29:58 PM5/10/12
to mongod...@googlegroups.com
Hello,

I can't get an upset to work with the Java driver. The upsert in question is on an _id which just for testing I set to 1000 for testing purposes.

BasicDBObject findCriterion = new BasicDBObject();
BasicDBObject incClause = new BasicDBObject();
BasicDBObject incDoc = new BasicDBObject();

... I populate the incClause and incDoc, the final query looks like this (toString)
// findCriterion: { "_id" : 1000}
// incClause: { "total" : 1 , "site.lentreprise|lexpress|fr" : 1}
// incDoc: { "$inc" : { "total" : 1 , "site.lentreprise|lexpress|fr" : 1}}

coll.update(findCriterion, incClause, true, false);

I expect to find something like doc = {'_id' : 1000, "site : {"lentreprise|express|fr" : 1}, "total" : 1} in the collection but it just inserts empty documents like { "_id" : ObjectId("4fabf4d7e1bd727dc2fca36d") }

What do I need to do in order to be able to perform upserts on the  _id field?

Thanks,

/David

Scott Hernandez

unread,
May 10, 2012, 1:43:43 PM5/10/12
to mongod...@googlegroups.com
Can you produce the behavior you want in the mongo javascript shell
and post the example?
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/KB6GnTd_CiMJ.
> 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.

David the Dude

unread,
May 10, 2012, 3:52:36 PM5/10/12
to mongod...@googlegroups.com
Sure,

$ mongo test

var myID = new BinData(3, "21ec20203aea1069a2dd08002b30309d");
var mySite = "lentreprise|lexpress|fr";
var incDoc = {};

incDoc["site." + mySite] = 1;
incDoc["total"] = 1;
db.blah.update({ "_id" : myID }, { $inc : incDoc }, upsert=true);
db.blah.findOne()

{
"_id" : BinData(3,"21ec20203aea1069a2dd08002b30309d"),
"site" : {
"lentreprise|lexpress|fr" : 1
},
"total" : 1
}

I reconstructed the exact same query in Java - the JSON I printed above was generated by calling .toString() on the DBObjects, yet - using the Java driver I find it impossible to upsert - at least on "_id" fields. I haven't found any complaints so I must be doing something wrong.

Thank you,

/David

David the Dude

unread,
May 10, 2012, 6:10:37 PM5/10/12
to mongod...@googlegroups.com
Never mind, it works now - I accidentally cleared incClause and incDoc before doing the actual upsert.
Reply all
Reply to author
Forward
0 new messages