-
Type: Improvement
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
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:
db.pg.find() [ { _id: 1, last_id: 84 }, { _id: 2, last_id: 60 }
The b2 collection is problematic. Here it is:
db.b2.find() [ { _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:
db.b3.find() [ { _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):
db.b2.getIndexes() [ { v: 2, key: { _id: 1 }, name: '_id_' } ]
db.b3.getIndexes() [ { 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"}} ]); db.b3.find() [ { _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.
- is duplicated by
-
SERVER-61612 $merge failed to update the matching document, did you attempt to modify the _id
- Closed
- mentioned in
-
Page Loading...