Files
mongo/jstests/core/query/project/projection_conflicts.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

39 lines
1.2 KiB
JavaScript

/**
* Test projections which have conflicts in them.
*/
const coll = db.projection_conflicts;
coll.drop();
assert.commandWorked(coll.insert({a: {b: 1}}));
function checkProjectionFailsWithCode(proj, codes) {
const collisionErrorCodes = [31250, 31249];
const err = assert.throws(() => coll.find({}, proj).toArray());
assert.contains(err.code, collisionErrorCodes, proj);
const aggErr = assert.throws(() => coll.aggregate({$project: proj}));
assert.contains(err.code, collisionErrorCodes, proj);
}
// Can't test cases like {a: 1, a: "$$REMOVE"} because JS will remove the duplicate keys.
checkProjectionFailsWithCode({"a.b": 1, a: {b: "$$REMOVE"}});
// Inclusion only.
checkProjectionFailsWithCode({"a.b": 1, a: {b: {c: 1}}});
checkProjectionFailsWithCode({a: {b: {c: 1}}, "a.b": 1});
checkProjectionFailsWithCode({a: 1, "a.b": 1});
checkProjectionFailsWithCode({"a.b": 1, a: 1});
checkProjectionFailsWithCode({"a.b": 1, "a.b.c": 1});
// Exclusion only.
checkProjectionFailsWithCode({"a.b": 0, a: {b: {c: 0}}});
checkProjectionFailsWithCode({a: {b: {c: 0}}, "a.b": 0});
checkProjectionFailsWithCode({a: 0, "a.b": 0});
checkProjectionFailsWithCode({"a.b": 0, a: 0});
checkProjectionFailsWithCode({"a.b": 0, "a.b.c": 0});