You cannot set _id
if dbobject
is just a document and does not contain an update operator eg: $set
, $setOnInsert
.
Just passing a document will replace the whole document meaning it doesn't set an _id
a falls back to ObjectId
So your example works if you use an update operator eg:
db.getCollection(collection).update(
new BasicDBObject("_id", "12"),
new BasicDBObject("$set", new BasicDBObject("Hi", "world")), true, false)