Files
mongo/jstests/aggregation/sources/unwind/unwind_include_array_index.js
Zac 591928c619 SERVER-108478 JS formatted by prettier and remove clang-format (#39656)
GitOrigin-RevId: 6c8f6aded47f260aa4f7c231b17dae3302cb1e04
2025-08-21 17:27:09 +00:00

54 lines
1.8 KiB
JavaScript

// SERVER-4588 Add option to $unwind to emit array index.
const coll = db[jsTestName()];
coll.drop();
assert.commandWorked(coll.insert({_id: 0}));
assert.commandWorked(coll.insert({_id: 1, x: null}));
assert.commandWorked(coll.insert({_id: 2, x: []}));
assert.commandWorked(coll.insert({_id: 3, x: [1, 2, 3]}));
assert.commandWorked(coll.insert({_id: 4, x: 5}));
// Without includeArrayIndex.
let actualResults = coll.aggregate([{$unwind: {path: "$x"}}, {$sort: {_id: 1, x: 1}}]).toArray();
let expectedResults = [
{_id: 3, x: 1},
{_id: 3, x: 2},
{_id: 3, x: 3},
{_id: 4, x: 5},
];
assert.eq(expectedResults, actualResults, "Incorrect results for normal $unwind");
// With includeArrayIndex, index inserted into a new field.
actualResults = coll
.aggregate([{$unwind: {path: "$x", includeArrayIndex: "index"}}, {$sort: {_id: 1, x: 1}}])
.toArray();
expectedResults = [
{_id: 3, x: 1, index: NumberLong(0)},
{_id: 3, x: 2, index: NumberLong(1)},
{_id: 3, x: 3, index: NumberLong(2)},
{_id: 4, x: 5, index: null},
];
assert.eq(expectedResults, actualResults, "Incorrect results $unwind with includeArrayIndex");
// With both includeArrayIndex and preserveNullAndEmptyArrays.
actualResults = coll
.aggregate([
{$unwind: {path: "$x", includeArrayIndex: "index", preserveNullAndEmptyArrays: true}},
{$sort: {_id: 1, x: 1}},
])
.toArray();
expectedResults = [
{_id: 0, index: null},
{_id: 1, x: null, index: null},
{_id: 2, index: null},
{_id: 3, x: 1, index: NumberLong(0)},
{_id: 3, x: 2, index: NumberLong(1)},
{_id: 3, x: 3, index: NumberLong(2)},
{_id: 4, x: 5, index: null},
];
assert.eq(
expectedResults,
actualResults,
"Incorrect results $unwind with includeArrayIndex and preserveNullAndEmptyArrays",
);