-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
-
Query Optimization
-
Fully Compatible
-
ALL
-
v8.0
-
200
Based on the docs for $push:
$push returns an array of all values that result from applying an expression to documents.
It was not clear to me that this is intended to guarantee any order in the output array.
As described in SERVER-85337, any order-sensitive accumulator needs to be handled specially in $bucketAuto, because $bucketAuto internally sorts by the group-key to choose bucket boundaries.
This means currently, $bucketAuto $push outputs elements sorted by group key instead of obeying the input order:
> db.c.aggregate([ {$sort: {_id: 1}}, {$bucketAuto: {groupBy: "$n", buckets: 1, output: {docs: {$push: "$$ROOT"}}}} ]).pretty() { "_id" : { "min" : 1, "max" : 10 }, "docs" : [ { "_id" : 9, "n" : 1 }, { "_id" : 8, "n" : 2 }, { "_id" : 7, "n" : 3 }, { "_id" : 6, "n" : 4 }, { "_id" : 5, "n" : 5 }, { "_id" : 4, "n" : 6 }, { "_id" : 3, "n" : 7 }, { "_id" : 2, "n" : 8 }, { "_id" : 1, "n" : 9 }, { "_id" : 0, "n" : 10 } ] }
If we want to provide this guarantee, we should fix $bucketAuto $push. And either way we should update the docs to explicitly guarantee or not-guarantee the order.
- is related to
-
SERVER-97362 $bucketAuto $concatArrays does not preserve order
- Closed