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

Enhance "Performing an update on the path '_id' would modify the immutable field '_id" error message

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      This is a very strange case - two identical collections behave in a completely different way.

      Initially I was trying to run $group with a merge and started getting _id errors in the title and I whittled the test case down to two collections and a simple aggregate pipeline. I can't see any difference between these collections to the point that I tried to BSON-dump them and data looks the same.

      The collection pg is a result of a $group aggregation saved in a collection to simplify things. Here's its contents:

      [ { _id: 1, last_id: 84 }, { _id: 2, last_id: 60 } 

      The b2 collection is problematic. Here it is:

        { _id: 1, created: ISODate("2021-11-18T23:16:33.149Z") },
        { _id: 2, created: ISODate("2021-11-18T23:16:33.149Z") }

      Here's the working collection b3:

        { _id: 1, created: ISODate("2021-11-18T22:53:02.113Z") },
        { _id: 2, created: ISODate("2021-11-18T22:53:02.113Z") }

      Here's indexes for both (to make sure it's not a collation issue):

      [ { v: 2, key: { _id: 1 }, name: '_id_' } ]
      [ { v: 2, key: { _id: 1 }, name: '_id_' } ] 

      Finally, here's the problem. When I run this command, it succeeds

      db.pg.aggregate([{$merge: {into: "b3", whenMatched: "merge", whenNotMatched: "fail"}} ]);
        { _id: 1, created: ISODate("2021-11-18T22:53:02.113Z"), last_id: 84 },
        { _id: 2, created: ISODate("2021-11-18T22:53:02.113Z"), last_id: 60 }

      When I run the same command against b2, it generates this error:

      db.pg.aggregate([{$merge: {into: "b2", whenMatched: "merge", whenNotMatched: "fail"}} ]);
      MongoServerError: $merge failed to update the matching document, did you attempt to modify the _id or the shard key? :: caused by :: Performing an update on the path '_id' would modify the immutable field '_id 

      In a real-life case, there's no pg collection and instead it's a $group stage that updates a collection with the grouping output.

      This is happening on an Atlas cluster v4.4.10. Stats for both collections are the same, so I'm not adding them to keep the post smaller. Let me know if you need any other information. I cannot see the difference between these collections in any of the outputs I could come up with.

            eric.sedor@mongodb.com Eric Sedor
            cis74633@bell.net Andre M
            0 Vote for this issue
            3 Start watching this issue