jstests/aggregation/sources/densify/partitions.js
// Three partitions, each with only one document.
Three partitions, each with only one document.
Three partitions each with only one document .
function fullTestTwoDates(stepVal = 2) {
    coll.drop();
    let testDocs = [];
    let testExpected = [];
    testDocs.push({val: new ISODate("2021-01-01"), part: 0});
    testDocs.push({val: new ISODate("2021-01-01"), part: 1});
    testDocs.push({val: new ISODate("2031-01-01"), part: 2});
    testDocs.push({val: new ISODate("2025-01-01"), part: 3});
    for (let densifyVal = 0; densifyVal < 11; densifyVal += stepVal) {
        for (let partitionVal = 0; partitionVal <= 3; partitionVal++) {
            testExpected.push({
                val: new ISODate((2021 + densifyVal).toString().padStart(2, '0') + "-01-01"),
                part: partitionVal
            });
        }
    }
    assert.commandWorked(coll.insert(testDocs));
    let result = coll.aggregate([
        {$project: {_id: 0}},
        {
            $densify: {
                field: "val",
                range: {step: stepVal, unit: "year", bounds: "full"},
                partitionByFields: ["part"]
            }
        },
        {$sort: {val: 1, part: 1}}
    ]);
    const resultArray = result.toArray();
    assert(arrayEq(resultArray, testExpected), buildErrorString(resultArray, testExpected));
}
function fullTestTwoDates ( stepVal = 2 ) { coll . drop ( ) ; let testDocs = [ ] ; let testExpected = [ ] ; testDocs . push ( { val : new ISODate ( "2021-01-01" ) , part : 0 } ) ; testDocs . push ( { val : new ISODate ( "2021-01-01" ) , part : 1 } ) ; testDocs . push ( { val : new ISODate ( "2031-01-01" ) , part : 2 } ) ; testDocs . push ( { val : new ISODate ( "2025-01-01" ) , part : 3 } ) ; for ( let densifyVal = 0 ; densifyVal < 11 ; densifyVal += stepVal ) { for ( let partitionVal = 0 ; partitionVal <= 3 ; partitionVal ++ ) { testExpected . push ( { val : new ISODate ( ( 2021 + densifyVal ) . toString ( ) . padStart ( 2 , '0' ) + "-01-01" ) , part : partitionVal } ) ; } } assert . commandWorked ( coll . insert ( testDocs ) ) ; let result = coll . aggregate ( [ { $project : { _id : 0 } } , { $densify : { field : "val" , range : { step : stepVal , unit : "year" , bounds : "full" } , partitionByFields : [ "part" ] } } , { $sort : { val : 1 , part : 1 } } ] ) ; const resultArray = result . toArray ( ) ; assert ( arrayEq ( resultArray , testExpected ) , buildErrorString ( resultArray , testExpected ) ) ; }
Three partitions, each with only one document.
Three partitions each with only one document .
// Add an initial document.