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

Unclarity in the definition of upsert

    XMLWordPrintableJSON

Details

    Description

      On this page:
      https://docs.mongodb.org/manual/reference/method/db.collection.update/#update-parameter

      If upsert is true and no document matches the query criteria, update() inserts a single document. The update creates the new document with either:

      • The fields and values of the <update> parameter if the <update> parameter contains only field and value pairs, or
      • The fields and values of both the <query> and <update> parameters if the <update> parameter contains update operator expressions. The update creates a base document from the equality clauses in the <query> parameter, and then applies the update expressions from the <update> parameter.

      Below on the same page:

      When you execute an update() with upsert: true and the query matches no existing document, MongoDB will refuse to insert a new document if the query specifies conditions on the _id field using dot notation.

      So it turns out that if the <query> contains the _id, then the document that will be inserted will always contain both the <query> and <update> despite on what is written above that this thing is done only when the <update> contains update operator expressions.

      Attachments

        Activity

          People

            Unassigned Unassigned
            xpromache Nicolae Mihalache
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              1 year, 14 weeks, 2 days ago