Files
mongo/jstests/aggregation/sources/setWindowFields/output_overwrites_existing_data.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

27 lines
1.1 KiB
JavaScript

/**
* Tests that $setWindowFields will overwrite existing fields in the output document if there are
* conflicts. This test was designed to confirm a behavior change from SERVER-63079.
* @tags: [
* # $documents is not supported inside a $facet.
* do_not_wrap_aggregations_in_facets,
* ]
*/
assert.commandWorked(db[jsTestName()].insert({dummy: 1}));
let windowResults = db.aggregate([
{$documents: [{_id: 0, obj: {k1: "v1", k2: "v2"}}]},
{$setWindowFields: {sortBy: {_id: 1}, output: {obj: {$last: {newSubObject: 1}}}}},
]);
// The 'newSubObject' should overwrite the existing 'k1' and 'k2' object.
assert.eq(windowResults.toArray(), [{_id: 0, obj: {newSubObject: 1}}]);
// Test that we can preserve the other fields by using a dotted notation and having the window
// function result in the target value (1 here) instead of an object literal.
windowResults = db.aggregate([
{$documents: [{_id: 0, obj: {k1: "v1", k2: "v2"}}]},
{$setWindowFields: {sortBy: {_id: 1}, output: {"obj.subPath": {$last: 1}}}},
]);
assert.eq(windowResults.toArray(), [{_id: 0, obj: {k1: "v1", k2: "v2", subPath: 1}}]);