|
The manual states the following:
When you specify upsert: true for an update operation to replace a document and no matching documents are found, MongoDB creates a new document using the equality conditions in the update conditions document, and replaces this document, except for the _id field if specified, with the update document.
This language seems accurate, but is probably not clear to someone just getting started. That is, I think they would be surprised that in this case, b is not set in the final document:
m101:PRIMARY> db.foo.update({b:1},{c:1},true)
|
WriteResult({
|
"nMatched" : 0,
|
"nUpserted" : 1,
|
"nModified" : 0,
|
"_id" : ObjectId("55778ef8765f4bc44534a00d")
|
})
|
m101:PRIMARY> db.foo.findOne()
|
{ "_id" : ObjectId("5535149e0c027883395faeb2"), "a" : 1 }
|
and yet, in this very similar case, _id is set to 0.
m101:PRIMARY> db.bar.update({_id:0},{c:1}, true);
|
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 0 })
|
m101:PRIMARY> db.bar.findOne()
|
{ "_id" : 0, "c" : 1 }
|
The mongo shell documentation is even less clear.

There is no mention that _id is handled specially in the upsert section.
|