Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-5594

MongoDB Manual and shell manual not clear on what happens on update with insert with respect to _id

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major - P3 Major - P3
    • 01112017-cleanup
    • mongodb-3.0
    • manual
    • None

    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.

      Attachments

        Activity

          People

            kay.kim@mongodb.com Kay Kim (Inactive)
            erlichson Andrew Erlichson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              8 years, 36 weeks, 1 day ago