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

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.
var local = db.local;
var foreign = db.foreign;
local.drop();
foreign.drop();
// 'connectFromField' is an array of objects.
var bulk = foreign.initializeUnorderedBulkOp();
for (var 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}));
var 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);