Problem
The same cause as SERVER-85337. Previously, $bucketAuto had incorrect implementation to assume stable-sort works well with positional accumulators like $first and $last. SERVER-85337 fixes the issue by specializing $first/$firstN/$last/$lastN. $mergeObject is a scapegoat only discovered recently after SERVER-81571 is merged and later revealed in fuzzer test failure BF-32056.
Steps to reproduce
db.c.insertMany([ { "_id" : 0, "a" : 0 }, { "_id" : 1, "a" : 1 }, { "_id" : 2, "a" : 2 }, { "_id" : 3, "a" : 3 }, { "_id" : 4, "a" : 4 }, { "_id" : 5, "a" : 5 }, { "_id" : 6, "a" : 6 }, { "_id" : 7, "a" : 7 }, { "_id" : 8, "a" : 8 }, { "_id" : 9, "a" : 9 }]); db.c.aggregate([ {$sort: {a: -1}}, {$bucket: {groupBy: "$a", boundaries: [0, 5, 10], output: {a: {$mergeObjects: "$$ROOT"}} }} ]); db.c.aggregate([ {$sort: {a: -1}}, {$bucketAuto: {groupBy: "$a", buckets: 2, output: {a: {$mergeObjects: "$$ROOT"}} }} ]);
$bucket returns the results:
{ "_id" : 0, "a" : { "_id" : 0, "a" : 0 } } { "_id" : 5, "a" : { "_id" : 5, "a" : 5 } }
Whereas $bucketAuto returns the results:
{ "_id" : { "min" : 0, "max" : 5 }, "a" : { "_id" : 4, "a" : 4 } } { "_id" : { "min" : 5, "max" : 9 }, "a" : { "_id" : 9, "a" : 9 } }
Plan of attack
Follow SERVER-85337 to specialize accumulator $mergeObject as well.
- is depended on by
-
SERVER-81571 Reconsider stable sort in sorter.cpp
- Closed
- is related to
-
SERVER-85337 $bucketAuto $first does not obey input order
- Closed