[DOCS-5594] MongoDB Manual and shell manual not clear on what happens on update with insert with respect to _id Created: 10/Jun/15  Updated: 11/Jan/17  Resolved: 12/Sep/16

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: mongodb-3.0
Fix Version/s: 01112017-cleanup

Type: Bug Priority: Major - P3
Reporter: Andrew Erlichson Assignee: Kay Kim (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File upsert.png    
Issue Links:
Related
is related to DOCS-6826 CRUD Tutorial Improvement Closed
Participants:
Days since reply: 8 years, 36 weeks, 1 day ago

 Description   

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.


Generated at Thu Feb 08 07:50:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.