Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-58185

Expected update pipeline behavior of $merge

    • Type: Icon: Question Question
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • QE 2021-08-23

      I have some sample data as follows:

      > db.first.find()
      { "_id" : 1, "flavor" : "chocolate" }
      
      
      > db.another.find()
      { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 }
      { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 }
      { "_id" : 3 }
      { "_id" : 4, "item" : "ginger" }
      

      When running a pipeline with $merge:

      db.first.aggregate([
        {
          $merge: {
            "into": "another",
            "whenMatched": [
              {
                "$project": {
                  "_id": 0
                }
              }
            ]
          }
        }
      ])
      

      This results in no error, and no updates to the "another" collection. Is this expected? I assumed this would not be allowed as there is a restriction on the "whenMatched" pipeline not updating the "on" field.

      Another example, using a different "on" field:

      > db.second.find()
      { "_id" : ObjectId("60dcae7e4a59a35f0ed6b34b"), "item" : "almonds" }
      
      
      > db.another.find()
      { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 }
      { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 }
      { "_id" : 3 }
      { "_id" : 4, "item" : "ginger" }
      

      Pipeline:

      db.second.aggregate([
        {
          $merge: {
            "into": "another",
            "whenMatched": [
              {
                "$project": {
                  "item": 0
                }
              }
            ]
          }
        }
      ])
      uncaught exception: Error: command failed: {
              "ok" : 0,
              "errmsg" : "E11000 duplicate key error collection: playground.another index: item_1 dup key: { item: \"almonds\" }",
              "code" : 11000,
              "codeName" : "DuplicateKey",
              "keyPattern" : {
                      "item" : 1
              },
              "keyValue" : {
                      "item" : "almonds"
              }
      } : aggregate failed :
      _getErrorWithCode@src/mongo/shell/utils.js:25:13
      doassert@src/mongo/shell/assert.js:18:14
      _assertCommandWorked@src/mongo/shell/assert.js:618:17
      assert.commandWorked@src/mongo/shell/assert.js:708:16
      DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12
      @(shell):1:1

      It seems that in this case I run into a duplicate key error as we attempt to insert a results document. What is the correct behavior in this case?

            Assignee:
            kateryna.kamenieva@mongodb.com Katya Kamenieva
            Reporter:
            alice.thum@mongodb.com Alice Thum
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: