1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
// Cannot implicitly shard accessed collections because unsupported use of sharded collection
// for target collection of $lookup and $graphLookup.
// @tags: [assumes_unsharded_collection]
// In SERVER-24714, the 'restrictSearchWithMatch' option was added to $graphLookup. In this file,
// we test the functionality and correctness of the option.
(function() {
"use strict";
var local = db.local;
var foreign = db.foreign;
local.drop();
foreign.drop();
var bulk = foreign.initializeUnorderedBulkOp();
for (var i = 0; i < 100; i++) {
bulk.insert({_id: i, neighbors: [i - 1, i + 1]});
}
assert.writeOK(bulk.execute());
assert.writeOK(local.insert({starting: 0}));
// Assert that the graphLookup only retrieves ten documents, with _id from 0 to 9.
var res = local
.aggregate({
$graphLookup: {
from: "foreign",
startWith: "$starting",
connectFromField: "neighbors",
connectToField: "_id",
as: "integers",
restrictSearchWithMatch: {_id: {$lt: 10}}
}
})
.toArray()[0];
assert.eq(res.integers.length, 10);
// Assert that the graphLookup doesn't retrieve any documents, as to do so it would need to
// traverse nodes in the graph that don't match the 'restrictSearchWithMatch' predicate.
res = local
.aggregate({
$graphLookup: {
from: "foreign",
startWith: "$starting",
connectFromField: "neighbors",
connectToField: "_id",
as: "integers",
restrictSearchWithMatch: {_id: {$gt: 10}}
}
})
.toArray()[0];
assert.eq(res.integers.length, 0);
foreign.drop();
assert.writeOK(foreign.insert({from: 0, to: 1, shouldBeIncluded: true}));
assert.writeOK(foreign.insert({from: 1, to: 2, shouldBeIncluded: false}));
assert.writeOK(foreign.insert({from: 2, to: 3, shouldBeIncluded: true}));
// Assert that the $graphLookup stops exploring when it finds a document that doesn't match the
// filter.
res = local
.aggregate({
$graphLookup: {
from: "foreign",
startWith: "$starting",
connectFromField: "to",
connectToField: "from",
as: "results",
restrictSearchWithMatch: {shouldBeIncluded: true}
}
})
.toArray()[0];
assert.eq(res.results.length, 1);
// $expr is allowed inside the 'restrictSearchWithMatch' match expression.
res = local
.aggregate({
$graphLookup: {
from: "foreign",
startWith: "$starting",
connectFromField: "to",
connectToField: "from",
as: "results",
restrictSearchWithMatch: {$expr: {$eq: ["$shouldBeIncluded", true]}}
}
})
.toArray()[0];
assert.eq(res.results.length, 1);
})();
|