jstests/aggregation/accumulators/min_n_max_n.js
// expected groups (we cannot perform unordered comparison because order matters for $minN/$maxN).
expected groups (we cannot perform unordered comparison because order matters for $minN/$maxN).
expected groups ( we cannot perform unordered comparison because order matters for $minN / $maxN ) .
function runAndCompareMinMaxN(nFunction, n, expectedResults) {
    const actualResults =
        coll.aggregate([
                {$group: {_id: '$state', sales: {[nFunction]: {input: '$sales', n: n}}}},
                {$sort: {_id: 1}}
            ])
            .toArray();
    assert.eq(expectedResults, actualResults);
    let actualBucketAutoResults =
        coll.aggregate([
                {
                    $bucketAuto: {
                        groupBy: '$state',
                        buckets: 10 * 1000,
                        output: {sales: {[nFunction]: {input: '$sales', n: n}}}
                    }
                },
                {$project: {_id: "$_id.min", sales: 1}},
                {$sort: {_id: 1}},
            ])
            .toArray();
    for (let i = 0; i < actualBucketAutoResults.length; ++i) {
        const currentDoc = actualBucketAutoResults[i];
        actualBucketAutoResults[i] = {_id: currentDoc._id, sales: currentDoc.sales};
    }
    assert.eq(expectedResults, actualBucketAutoResults);
}
function runAndCompareMinMaxN ( nFunction , n , expectedResults ) { const actualResults = coll . aggregate ( [ { $group : { _id : '$state' , sales : { [ nFunction ] : { input : '$sales' , n : n } } } } , { $sort : { _id : 1 } } ] ) . toArray ( ) ; assert . eq ( expectedResults , actualResults ) ; let actualBucketAutoResults = coll . aggregate ( [ { $bucketAuto : { groupBy : '$state' , buckets : 10 * 1000 , output : { sales : { [ nFunction ] : { input : '$sales' , n : n } } } } } , { $project : { _id : "$_id.min" , sales : 1 } } , { $sort : { _id : 1 } } , ] ) . toArray ( ) ; for ( let i = 0 ; i < actualBucketAutoResults . length ; ++ i ) { const currentDoc = actualBucketAutoResults [ i ] ; actualBucketAutoResults [ i ] = { _id : currentDoc . _id , sales : currentDoc . sales } ; } assert . eq ( expectedResults , actualBucketAutoResults ) ; }
expected groups (we cannot perform unordered comparison because order matters for $minN/$maxN).
expected groups ( we cannot perform unordered comparison because order matters for $minN / $maxN ) .
// Basic correctness test for $minN/$maxN used in $bucketAuto. Though $bucketAuto uses // accumulators in the same way that $group does, the test below verifies that everything // works properly with serialization and reporting results. Note that the $project allows us // to compare the $bucketAuto results to the expected $group results (because there are more // buckets than groups, it will always be the case that the min value of each bucket // corresponds to the group key). // Using a computed projection will put the fields out of order. As such, we re-order them // below.