Files
mongo/jstests/aggregation/sources/graphLookup/nested_objects.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

78 lines
2.0 KiB
JavaScript

// In MongoDB 3.4, $graphLookup was introduced. In this file, we test the behavior of graphLookup
// when the 'connectToField' is a nested array, or when the 'connectFromField' is a nested array.
let local = db.local;
let foreign = db.foreign;
local.drop();
foreign.drop();
// 'connectFromField' is an array of objects.
let bulk = foreign.initializeUnorderedBulkOp();
for (let i = 0; i < 100; i++) {
bulk.insert({_id: i, neighbors: [{id: i + 1}, {id: i + 2}]});
}
assert.commandWorked(bulk.execute());
assert.commandWorked(local.insert({starting: 0}));
let res = local
.aggregate({
$graphLookup: {
from: "foreign",
startWith: "$starting",
connectFromField: "neighbors.id",
connectToField: "_id",
as: "integers",
},
})
.toArray()[0];
assert.eq(res.integers.length, 100);
foreign.drop();
// 'connectToField' is an array of objects.
bulk = foreign.initializeUnorderedBulkOp();
for (let i = 0; i < 100; i++) {
bulk.insert({previous: [{neighbor: i}, {neighbor: i - 1}], value: i + 1});
}
assert.commandWorked(bulk.execute());
res = local
.aggregate({
$graphLookup: {
from: "foreign",
startWith: "$starting",
connectFromField: "value",
connectToField: "previous.neighbor",
as: "integers",
},
})
.toArray()[0];
assert.eq(res.integers.length, 100);
foreign.drop();
// Both 'connectToField' and 'connectFromField' are arrays of objects.
bulk = foreign.initializeUnorderedBulkOp();
for (let i = 0; i < 100; i++) {
bulk.insert({
previous: [{neighbor: i}, {neighbor: i - 1}],
values: [{neighbor: i + 1}, {neighbor: i + 2}],
});
}
assert.commandWorked(bulk.execute());
res = local
.aggregate({
$graphLookup: {
from: "foreign",
startWith: "$starting",
connectFromField: "values.neighbor",
connectToField: "previous.neighbor",
as: "integers",
},
})
.toArray()[0];
assert.eq(res.integers.length, 100);