Files
mongo/jstests/aggregation/sources/graphLookup/missing_fields.js
Steve McClure 1ffbc6c2e9 SERVER-109432: Autofix JS var usage to favor let (#40637)
GitOrigin-RevId: 9674b7db36a0f3f650d39c1e3fb2ad6ff2141cfb
2025-08-28 19:21:01 +00:00

138 lines
3.5 KiB
JavaScript

// Test that missing values are not returned when the connectFrom value in a $graphLookup is null.
import {arrayEq} from "jstests/aggregation/extras/utils.js";
let local = db[jsTestName()];
local.drop();
assert.commandWorked(
local.insert([
{"_id": 0, "x": 1, "y": 1},
{"_id": 1, "x": 1, "y": null},
{"_id": 2, "x": 1, "y": 1},
{"_id": 3, "x": 1},
]),
);
let result = local
.aggregate([
{
$graphLookup: {
from: local.getName(),
startWith: "$y",
connectFromField: "x",
connectToField: "y",
as: "arr",
maxDepth: 0,
},
},
])
.toArray();
let expected = [
{
"_id": 0,
"x": 1,
"y": 1,
"arr": [
{"_id": 2, "x": 1, "y": 1},
{"_id": 0, "x": 1, "y": 1},
],
},
{"_id": 1, "x": 1, "y": null, "arr": [{"_id": 1, "x": 1, "y": null}]},
{
"_id": 2,
"x": 1,
"y": 1,
"arr": [
{"_id": 2, "x": 1, "y": 1},
{"_id": 0, "x": 1, "y": 1},
],
},
{"_id": 3, "x": 1, "arr": []},
];
assert(arrayEq(expected, result), "Expected:\n" + tojson(expected) + "\nGot:\n" + tojson(result));
assert.commandWorked(local.insert({"_id": 4, "x": 1, "y": undefined}));
result = local
.aggregate([
{
$graphLookup: {
from: local.getName(),
startWith: null,
connectFromField: "x",
connectToField: "y",
as: "arr",
maxDepth: 0,
},
},
])
.toArray();
expected = [
{"_id": 0, "x": 1, "y": 1, "arr": [{"_id": 1, "x": 1, "y": null}]},
{"_id": 1, "x": 1, "y": null, "arr": [{"_id": 1, "x": 1, "y": null}]},
{"_id": 2, "x": 1, "y": 1, "arr": [{"_id": 1, "x": 1, "y": null}]},
{"_id": 3, "x": 1, "arr": [{"_id": 1, "x": 1, "y": null}]},
{"_id": 4, "x": 1, "y": undefined, "arr": [{"_id": 1, "x": 1, "y": null}]},
];
assert(arrayEq(expected, result), "Expected:\n" + tojson(expected) + "\nGot:\n" + tojson(result));
local.drop();
// Test missing doesn't match missing.
assert.commandWorked(
local.insert([
{"_id": 0, "x": 1, "y": 1},
{"_id": 1, "x": 2},
{"_id": 3, "x": 1},
]),
);
result = local
.aggregate([
{
$graphLookup: {
from: local.getName(),
startWith: "$y",
connectFromField: "x",
connectToField: "y",
as: "arr",
maxDepth: 0,
},
},
])
.toArray();
expected = [
{"_id": 0, "x": 1, "y": 1, "arr": [{"_id": 0, "x": 1, "y": 1}]},
{"_id": 1, "x": 2, "arr": []},
{"_id": 3, "x": 1, "arr": []},
];
assert(arrayEq(expected, result));
local.drop();
assert.commandWorked(
local.insert([
{"_id": 0, "x": 1},
{"_id": 1, "x": 2},
{"_id": 3, "x": 1},
]),
);
result = local
.aggregate([
{
$graphLookup: {
from: local.getName(),
startWith: "$y",
connectFromField: "x",
connectToField: "y",
as: "arr",
maxDepth: 0,
},
},
])
.toArray();
expected = [
{"_id": 0, "x": 1, "arr": []},
{"_id": 1, "x": 2, "arr": []},
{"_id": 3, "x": 1, "arr": []},
];
assert(arrayEq(expected, result), "Expected:\n" + tojson(expected) + "\nGot:\n" + tojson(result));