Files
mongo/jstests/aggregation/sources/unionWith/unionWith_invalid_usage.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

79 lines
2.7 KiB
JavaScript

/**
* Tests for invalid usages of $unionWith, or invalid stages within the $unionWith sub-pipeline.
* @tags: [
* # Some stages we're checking are only supported with a single read concern.
* assumes_read_concern_unchanged
* ]
*/
import {FixtureHelpers} from "jstests/libs/fixture_helpers.js";
const baseColl = db["base"];
baseColl.drop();
const unionColl = db["union"];
unionColl.drop();
// Ensure the base collection exists.
assert.commandWorked(baseColl.insert({a: 1}));
// Disallowed within an update pipeline.
assert.commandFailedWithCode(baseColl.update({a: 1}, [{$unionWith: unionColl.getName()}]), [
ErrorCodes.InvalidOptions,
ErrorCodes.OperationNotSupportedInTransaction,
]);
function assertFailsWithCode(pipeline, errCode) {
assert.commandFailedWithCode(
db.runCommand({
aggregate: baseColl.getName(),
pipeline: pipeline,
cursor: {},
}),
errCode,
);
}
// Change streams are only supported against a replica set.
if (FixtureHelpers.isReplSet(db) || FixtureHelpers.isSharded(baseColl)) {
// Disallowed alongside a $changeStream.
assertFailsWithCode([{$changeStream: {}}, {$unionWith: unionColl.getName()}], ErrorCodes.IllegalOperation);
// Likewise, $changeStream is disallowed within a $unionWith sub-pipeline.
assertFailsWithCode([{$unionWith: {coll: unionColl.getName(), pipeline: [{$changeStream: {}}]}}], 31441);
assert.commandFailedWithCode(
db.runCommand({
aggregate: 1,
pipeline: [{$changeStream: {}}, {$unionWith: unionColl.getName()}],
cursor: {},
}),
ErrorCodes.IllegalOperation,
);
}
// $unionWith sub-pipeline cannot contain stages which write data ($merge, $out).
let subPipe = [{$out: "some_out_coll"}];
assertFailsWithCode([{$unionWith: {coll: unionColl.getName(), pipeline: subPipe}}], 31441);
subPipe = [
{
$merge: {
into: {db: db.getName(), coll: "some_merge_coll"},
whenMatched: "replace",
whenNotMatched: "fail",
},
},
];
assertFailsWithCode([{$unionWith: {coll: unionColl.getName(), pipeline: subPipe}}], 31441);
// Test that collection-less stages are not allowed within the $unionWith sub-pipeline.
subPipe = [{$listCachedAndActiveUsers: {}}];
assertFailsWithCode([{$unionWith: {coll: unionColl.getName(), pipeline: subPipe}}], ErrorCodes.InvalidNamespace);
subPipe = [{$listLocalSessions: {}}];
assertFailsWithCode([{$unionWith: {coll: unionColl.getName(), pipeline: subPipe}}], ErrorCodes.InvalidNamespace);
if (FixtureHelpers.isSharded(baseColl)) {
subPipe = [{$currentOp: {localOps: true}}];
assertFailsWithCode([{$unionWith: {coll: unionColl.getName(), pipeline: subPipe}}], ErrorCodes.InvalidNamespace);
}