
OPERATIONAL DEFECT DATABASE
...

...
> db.foo.drop() db.aggregate([{$documents:[{z: 1, a: 1}]}, {$merge: {into: "foo", whenMatched:"keepExisting", whenNotMatched:"insert"}}]) db.foo.find() When I run the above I get: [ { _id: ObjectId('68baf740fcc543022431a453'), a: 1, z: 1 } ] Note that `find` shows the `a` field first, even though the aggregation has `z` first. Per max.hirschhorn@mongodb.com, this bug is specific to this part of whenMatched/whenNotMatched settings. It does not happen, for example, if `whenMatched` is `replace`. Workaround whenMatched: [{$replaceWith: "$$ROOT"}]
max.hirschhorn@10gen.com commented on Fri, 5 Sep 2025 15:37:44 +0000: The reason why I consider this a bug is because we have a parameter value named "insert" in $merge and SERVER-74408 acknowledges that insertOne() and replaceOne() will preserve field order.
See above.
Click on a version to see all relevant bugs
MongoDB Integration
Learn more about where this data comes from
Bug Scrub Advisor
Streamline upgrades with automated vendor bug scrubs
BugZero Enterprise
Wish you caught this bug sooner? Get proactive today.