diff options
Diffstat (limited to 'jstests/aggregation/sources/graphLookup')
-rw-r--r-- | jstests/aggregation/sources/graphLookup/airports.js | 78 | ||||
-rw-r--r-- | jstests/aggregation/sources/graphLookup/basic.js | 133 | ||||
-rw-r--r-- | jstests/aggregation/sources/graphLookup/collation_graphlookup.js | 108 | ||||
-rw-r--r-- | jstests/aggregation/sources/graphLookup/error.js | 253 | ||||
-rw-r--r-- | jstests/aggregation/sources/graphLookup/filter.js | 58 | ||||
-rw-r--r-- | jstests/aggregation/sources/graphLookup/nested_objects.js | 70 | ||||
-rw-r--r-- | jstests/aggregation/sources/graphLookup/socialite.js | 46 | ||||
-rw-r--r-- | jstests/aggregation/sources/graphLookup/variables.js | 31 |
8 files changed, 386 insertions, 391 deletions
diff --git a/jstests/aggregation/sources/graphLookup/airports.js b/jstests/aggregation/sources/graphLookup/airports.js index 9254fd992fa..779678b07da 100644 --- a/jstests/aggregation/sources/graphLookup/airports.js +++ b/jstests/aggregation/sources/graphLookup/airports.js @@ -5,36 +5,36 @@ // In MongoDB 3.4, $graphLookup was introduced. In this file, we test some complex graphs. (function() { - "use strict"; +"use strict"; - var local = db.local; - var foreign = db.foreign; +var local = db.local; +var foreign = db.foreign; - local.drop(); - foreign.drop(); +local.drop(); +foreign.drop(); - var airports = [ - {_id: "JFK", connects: ["PWM", "BOS", "LGA", "SFO"]}, - {_id: "PWM", connects: ["BOS", "JFK"]}, - {_id: "BOS", connects: ["PWM", "JFK", "LGA"]}, - {_id: "SFO", connects: ["JFK", "MIA"]}, - {_id: "LGA", connects: ["BOS", "JFK", "ORD"]}, - {_id: "ORD", connects: ["LGA"]}, - {_id: "ATL", connects: ["MIA"]}, - {_id: "MIA", connects: ["ATL", "SFO"]} - ]; +var airports = [ + {_id: "JFK", connects: ["PWM", "BOS", "LGA", "SFO"]}, + {_id: "PWM", connects: ["BOS", "JFK"]}, + {_id: "BOS", connects: ["PWM", "JFK", "LGA"]}, + {_id: "SFO", connects: ["JFK", "MIA"]}, + {_id: "LGA", connects: ["BOS", "JFK", "ORD"]}, + {_id: "ORD", connects: ["LGA"]}, + {_id: "ATL", connects: ["MIA"]}, + {_id: "MIA", connects: ["ATL", "SFO"]} +]; - var bulk = foreign.initializeUnorderedBulkOp(); - airports.forEach(function(a) { - bulk.insert(a); - }); - assert.writeOK(bulk.execute()); +var bulk = foreign.initializeUnorderedBulkOp(); +airports.forEach(function(a) { + bulk.insert(a); +}); +assert.writeOK(bulk.execute()); - // Insert a dummy document so that something will flow through the pipeline. - local.insert({}); +// Insert a dummy document so that something will flow through the pipeline. +local.insert({}); - // Perform a simple $graphLookup and ensure it retrieves every result. - var res = local +// Perform a simple $graphLookup and ensure it retrieves every result. +var res = local .aggregate({ $graphLookup: { from: "foreign", @@ -46,12 +46,12 @@ }) .toArray()[0]; - // "foreign" represents a connected graph. - assert.eq(res.connections.length, airports.length); +// "foreign" represents a connected graph. +assert.eq(res.connections.length, airports.length); - // Perform a $graphLookup and ensure it correctly computes the shortest path to a node when more - // than one path exists. - res = local +// Perform a $graphLookup and ensure it correctly computes the shortest path to a node when more +// than one path exists. +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -66,17 +66,17 @@ {$project: {_id: "$connections._id", hops: "$connections.hops"}}) .toArray(); - var expectedDistances = {BOS: 0, PWM: 1, JFK: 1, LGA: 1, ORD: 2, SFO: 2, MIA: 3, ATL: 4}; +var expectedDistances = {BOS: 0, PWM: 1, JFK: 1, LGA: 1, ORD: 2, SFO: 2, MIA: 3, ATL: 4}; - assert.eq(res.length, airports.length); - res.forEach(function(c) { - assert.eq(c.hops, expectedDistances[c._id]); - }); +assert.eq(res.length, airports.length); +res.forEach(function(c) { + assert.eq(c.hops, expectedDistances[c._id]); +}); - // Disconnect the graph, and ensure we don't find the other side. - foreign.remove({_id: "JFK"}); +// Disconnect the graph, and ensure we don't find the other side. +foreign.remove({_id: "JFK"}); - res = db.local +res = db.local .aggregate({ $graphLookup: { from: "foreign", @@ -88,6 +88,6 @@ }) .toArray()[0]; - // ATL should now connect to itself, MIA, and SFO. - assert.eq(res.connections.length, 3); +// ATL should now connect to itself, MIA, and SFO. +assert.eq(res.connections.length, 3); }()); diff --git a/jstests/aggregation/sources/graphLookup/basic.js b/jstests/aggregation/sources/graphLookup/basic.js index c0bcb1a8a53..ef44b9b60bb 100644 --- a/jstests/aggregation/sources/graphLookup/basic.js +++ b/jstests/aggregation/sources/graphLookup/basic.js @@ -6,16 +6,16 @@ // of the stage. (function() { - "use strict"; +"use strict"; - var local = db.local; - var foreign = db.foreign; +var local = db.local; +var foreign = db.foreign; - local.drop(); - foreign.drop(); +local.drop(); +foreign.drop(); - // Ensure a $graphLookup works even if one of the involved collections doesn't exist. - const basicGraphLookup = { +// Ensure a $graphLookup works even if one of the involved collections doesn't exist. +const basicGraphLookup = { $graphLookup: { from: "foreign", startWith: "$starting", @@ -25,40 +25,39 @@ } }; - assert.eq( - local.aggregate([basicGraphLookup]).toArray().length, - 0, - "expected an empty result set for a $graphLookup with non-existent local and foreign " + - "collections"); +assert.eq(local.aggregate([basicGraphLookup]).toArray().length, + 0, + "expected an empty result set for a $graphLookup with non-existent local and foreign " + + "collections"); - assert.writeOK(foreign.insert({})); +assert.writeOK(foreign.insert({})); - assert.eq(local.aggregate([basicGraphLookup]).toArray().length, - 0, - "expected an empty result set for a $graphLookup on a non-existent local collection"); +assert.eq(local.aggregate([basicGraphLookup]).toArray().length, + 0, + "expected an empty result set for a $graphLookup on a non-existent local collection"); - local.drop(); - foreign.drop(); +local.drop(); +foreign.drop(); - assert.writeOK(local.insert({_id: 0})); +assert.writeOK(local.insert({_id: 0})); - assert.eq(local.aggregate([basicGraphLookup]).toArray(), - [{_id: 0, results: []}], - "expected $graphLookup to succeed with a non-existent foreign collection"); +assert.eq(local.aggregate([basicGraphLookup]).toArray(), + [{_id: 0, results: []}], + "expected $graphLookup to succeed with a non-existent foreign collection"); - local.drop(); - foreign.drop(); +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()); +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: 50})); +assert.writeOK(local.insert({starting: 50})); - // Perform a simple $graphLookup and ensure it retrieves every result. - var res = local +// Perform a simple $graphLookup and ensure it retrieves every result. +var res = local .aggregate({ $graphLookup: { from: "foreign", @@ -70,10 +69,10 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 100); +assert.eq(res.integers.length, 100); - // Perform a $graphLookup and ensure it respects "maxDepth". - res = local +// Perform a $graphLookup and ensure it respects "maxDepth". +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -86,11 +85,11 @@ }) .toArray()[0]; - // At depth zero, we retrieve one integer, and two for every depth thereafter. - assert.eq(res.integers.length, 11); +// At depth zero, we retrieve one integer, and two for every depth thereafter. +assert.eq(res.integers.length, 11); - // Perform a $graphLookup and ensure it properly evaluates "startWith". - res = local +// Perform a $graphLookup and ensure it properly evaluates "startWith". +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -103,11 +102,11 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 1); - assert.eq(res.integers[0]._id, 53); +assert.eq(res.integers.length, 1); +assert.eq(res.integers[0]._id, 53); - // Perform a $graphLookup and ensure it properly expands "startWith". - res = local +// Perform a $graphLookup and ensure it properly expands "startWith". +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -120,17 +119,17 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 3); +assert.eq(res.integers.length, 3); - // $graphLookup should not recurse when the 'connectFromField' is missing. However, if it - // mistakenly does, then it would look for a 'connectToField' value of null. In order to prevent - // regressions, we insert a document with a 'connectToField' value of null, then perform a - // $graphLookup, and ensure that we do not find the erroneous document. - assert.writeOK(foreign.remove({_id: 51})); - assert.writeOK(foreign.insert({_id: 51})); - assert.writeOK(foreign.insert({_id: null, neighbors: [50, 52]})); +// $graphLookup should not recurse when the 'connectFromField' is missing. However, if it +// mistakenly does, then it would look for a 'connectToField' value of null. In order to prevent +// regressions, we insert a document with a 'connectToField' value of null, then perform a +// $graphLookup, and ensure that we do not find the erroneous document. +assert.writeOK(foreign.remove({_id: 51})); +assert.writeOK(foreign.insert({_id: 51})); +assert.writeOK(foreign.insert({_id: null, neighbors: [50, 52]})); - res = local +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -142,17 +141,17 @@ }) .toArray()[0]; - // Our result should be missing the values with _id from 52 to 99. - assert.eq(res.integers.length, 52); +// Our result should be missing the values with _id from 52 to 99. +assert.eq(res.integers.length, 52); - // Perform a $graphLookup and ensure we don't go into an infinite loop when our graph is cyclic. - assert.writeOK(foreign.remove({_id: {$in: [null, 51]}})); - assert.writeOK(foreign.insert({_id: 51, neighbors: [50, 52]})); +// Perform a $graphLookup and ensure we don't go into an infinite loop when our graph is cyclic. +assert.writeOK(foreign.remove({_id: {$in: [null, 51]}})); +assert.writeOK(foreign.insert({_id: 51, neighbors: [50, 52]})); - assert.writeOK(foreign.update({_id: 99}, {$set: {neighbors: [98, 0]}})); - assert.writeOK(foreign.update({_id: 0}, {$set: {neighbors: [99, 1]}})); +assert.writeOK(foreign.update({_id: 99}, {$set: {neighbors: [98, 0]}})); +assert.writeOK(foreign.update({_id: 0}, {$set: {neighbors: [99, 1]}})); - res = local +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -164,10 +163,10 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 100); +assert.eq(res.integers.length, 100); - // Perform a $graphLookup and ensure that "depthField" is properly populated. - res = local +// Perform a $graphLookup and ensure that "depthField" is properly populated. +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -180,9 +179,9 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 100); +assert.eq(res.integers.length, 100); - res.integers.forEach(function(n) { - assert.eq(n.distance, Math.abs(50 - n._id)); - }); +res.integers.forEach(function(n) { + assert.eq(n.distance, Math.abs(50 - n._id)); +}); }()); diff --git a/jstests/aggregation/sources/graphLookup/collation_graphlookup.js b/jstests/aggregation/sources/graphLookup/collation_graphlookup.js index 7b457289cc6..f3fbcf2ee34 100644 --- a/jstests/aggregation/sources/graphLookup/collation_graphlookup.js +++ b/jstests/aggregation/sources/graphLookup/collation_graphlookup.js @@ -8,22 +8,26 @@ * set on the aggregation, or the default collation of the collection. */ (function() { - "use strict"; +"use strict"; - var res; - const caseInsensitiveUS = {collation: {locale: "en_US", strength: 2}}; - const caseSensitiveUS = {collation: {locale: "en_US", strength: 3}}; +var res; +const caseInsensitiveUS = { + collation: {locale: "en_US", strength: 2} +}; +const caseSensitiveUS = { + collation: {locale: "en_US", strength: 3} +}; - var coll = db.collation_graphlookup; - var foreignColl = db.collation_graphlookup_foreign; +var coll = db.collation_graphlookup; +var foreignColl = db.collation_graphlookup_foreign; - // Test that $graphLookup respects the collation set on the aggregation pipeline. Case - // insensitivity should mean that we find both "jeremy" and "jimmy" as friends. - coll.drop(); - assert.writeOK(coll.insert({username: "erica", friends: ["jeremy", "jimmy"]})); - assert.writeOK(coll.insert([{username: "JEREMY"}, {username: "JIMMY"}])); +// Test that $graphLookup respects the collation set on the aggregation pipeline. Case +// insensitivity should mean that we find both "jeremy" and "jimmy" as friends. +coll.drop(); +assert.writeOK(coll.insert({username: "erica", friends: ["jeremy", "jimmy"]})); +assert.writeOK(coll.insert([{username: "JEREMY"}, {username: "JIMMY"}])); - res = coll.aggregate( +res = coll.aggregate( [ {$match: {username: "erica"}}, { @@ -38,12 +42,12 @@ ], caseInsensitiveUS) .toArray(); - assert.eq(1, res.length); - assert.eq("erica", res[0].username); - assert.eq(2, res[0].friendUsers.length); +assert.eq(1, res.length); +assert.eq("erica", res[0].username); +assert.eq(2, res[0].friendUsers.length); - // Negative test: ensure that we don't find any friends when the collation is simple. - res = coll.aggregate([ +// Negative test: ensure that we don't find any friends when the collation is simple. +res = coll.aggregate([ {$match: {username: "erica"}}, { $graphLookup: { @@ -56,20 +60,20 @@ } ]) .toArray(); - assert.eq(1, res.length); - assert.eq("erica", res[0].username); - assert.eq(0, res[0].friendUsers.length); +assert.eq(1, res.length); +assert.eq("erica", res[0].username); +assert.eq(0, res[0].friendUsers.length); - coll.drop(); - assert.commandWorked(db.createCollection(coll.getName(), caseInsensitiveUS)); - assert.writeOK(coll.insert({username: "erica", friends: ["jeremy", "jimmy"]})); - foreignColl.drop(); - assert.commandWorked(db.createCollection(foreignColl.getName(), caseSensitiveUS)); - assert.writeOK(foreignColl.insert([{username: "JEREMY"}, {username: "JIMMY"}])); +coll.drop(); +assert.commandWorked(db.createCollection(coll.getName(), caseInsensitiveUS)); +assert.writeOK(coll.insert({username: "erica", friends: ["jeremy", "jimmy"]})); +foreignColl.drop(); +assert.commandWorked(db.createCollection(foreignColl.getName(), caseSensitiveUS)); +assert.writeOK(foreignColl.insert([{username: "JEREMY"}, {username: "JIMMY"}])); - // Test that $graphLookup inherits the default collation of the collection on which it is run, - // and that this collation is used instead of the default collation of the foreign collection. - res = coll.aggregate([ +// Test that $graphLookup inherits the default collation of the collection on which it is run, +// and that this collation is used instead of the default collation of the foreign collection. +res = coll.aggregate([ {$match: {username: "erica"}}, { $graphLookup: { @@ -82,18 +86,18 @@ } ]) .toArray(); - assert.eq(1, res.length); - assert.eq("erica", res[0].username); - assert.eq(2, res[0].friendUsers.length); +assert.eq(1, res.length); +assert.eq("erica", res[0].username); +assert.eq(2, res[0].friendUsers.length); - // Test that we don't use the collation to dedup string _id values. This would cause us to miss - // nodes in the graph that have distinct _id values which compare equal under the collation. - coll.drop(); - assert.writeOK(coll.insert({username: "erica", friends: ["jeremy"]})); - assert.writeOK(coll.insert({_id: "foo", username: "JEREMY", friends: ["jimmy"]})); - assert.writeOK(coll.insert({_id: "FOO", username: "jimmy", friends: []})); +// Test that we don't use the collation to dedup string _id values. This would cause us to miss +// nodes in the graph that have distinct _id values which compare equal under the collation. +coll.drop(); +assert.writeOK(coll.insert({username: "erica", friends: ["jeremy"]})); +assert.writeOK(coll.insert({_id: "foo", username: "JEREMY", friends: ["jimmy"]})); +assert.writeOK(coll.insert({_id: "FOO", username: "jimmy", friends: []})); - res = coll.aggregate( +res = coll.aggregate( [ {$match: {username: "erica"}}, { @@ -108,18 +112,18 @@ ], caseInsensitiveUS) .toArray(); - assert.eq(1, res.length); - assert.eq("erica", res[0].username); - assert.eq(2, res[0].friendUsers.length); +assert.eq(1, res.length); +assert.eq("erica", res[0].username); +assert.eq(2, res[0].friendUsers.length); - // Test that the result set is not deduplicated under the collation. If two documents are - // entirely equal under the collation, they should still both get returned in the "as" field. - coll.drop(); - assert.writeOK(coll.insert({username: "erica", friends: ["jeremy"]})); - assert.writeOK(coll.insert({_id: "foo", username: "jeremy"})); - assert.writeOK(coll.insert({_id: "FOO", username: "JEREMY"})); +// Test that the result set is not deduplicated under the collation. If two documents are +// entirely equal under the collation, they should still both get returned in the "as" field. +coll.drop(); +assert.writeOK(coll.insert({username: "erica", friends: ["jeremy"]})); +assert.writeOK(coll.insert({_id: "foo", username: "jeremy"})); +assert.writeOK(coll.insert({_id: "FOO", username: "JEREMY"})); - res = coll.aggregate( +res = coll.aggregate( [ {$match: {username: "erica"}}, { @@ -134,7 +138,7 @@ ], caseInsensitiveUS) .toArray(); - assert.eq(1, res.length); - assert.eq("erica", res[0].username); - assert.eq(2, res[0].friendUsers.length); +assert.eq(1, res.length); +assert.eq("erica", res[0].username); +assert.eq(2, res[0].friendUsers.length); })(); diff --git a/jstests/aggregation/sources/graphLookup/error.js b/jstests/aggregation/sources/graphLookup/error.js index 42d1203238c..b7360f3e9e8 100644 --- a/jstests/aggregation/sources/graphLookup/error.js +++ b/jstests/aggregation/sources/graphLookup/error.js @@ -6,18 +6,17 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". (function() { - "use strict"; +"use strict"; - var local = db.local; +var local = db.local; - local.drop(); - assert.writeOK(local.insert({b: 0})); +local.drop(); +assert.writeOK(local.insert({b: 0})); - var pipeline = {$graphLookup: 4}; - assertErrorCode( - local, pipeline, ErrorCodes.FailedToParse, "$graphLookup spec must be an object"); +var pipeline = {$graphLookup: 4}; +assertErrorCode(local, pipeline, ErrorCodes.FailedToParse, "$graphLookup spec must be an object"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -27,9 +26,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". maxDepth: "string" } }; - assertErrorCode(local, pipeline, 40100, "maxDepth must be numeric"); +assertErrorCode(local, pipeline, 40100, "maxDepth must be numeric"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -39,9 +38,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". maxDepth: -1 } }; - assertErrorCode(local, pipeline, 40101, "maxDepth must be nonnegative"); +assertErrorCode(local, pipeline, 40101, "maxDepth must be nonnegative"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -51,9 +50,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". maxDepth: 2.3 } }; - assertErrorCode(local, pipeline, 40102, "maxDepth must be representable as a long long"); +assertErrorCode(local, pipeline, 40102, "maxDepth must be representable as a long long"); - pipeline = { +pipeline = { $graphLookup: { from: -1, startWith: {$literal: 0}, @@ -62,9 +61,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". as: "output" } }; - assertErrorCode(local, pipeline, ErrorCodes.FailedToParse, "from must be a string"); +assertErrorCode(local, pipeline, ErrorCodes.FailedToParse, "from must be a string"); - pipeline = { +pipeline = { $graphLookup: { from: "", startWith: {$literal: 0}, @@ -73,9 +72,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". as: "output" } }; - assertErrorCode(local, pipeline, ErrorCodes.InvalidNamespace, "from must be a valid namespace"); +assertErrorCode(local, pipeline, ErrorCodes.InvalidNamespace, "from must be a valid namespace"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -84,9 +83,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". as: 0 } }; - assertErrorCode(local, pipeline, 40103, "as must be a string"); +assertErrorCode(local, pipeline, 40103, "as must be a string"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -95,9 +94,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". as: "$output" } }; - assertErrorCode(local, pipeline, 16410, "as cannot be a fieldPath"); +assertErrorCode(local, pipeline, 16410, "as cannot be a fieldPath"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -106,9 +105,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". as: "output" } }; - assertErrorCode(local, pipeline, 40103, "connectFromField must be a string"); +assertErrorCode(local, pipeline, 40103, "connectFromField must be a string"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -117,9 +116,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". as: "output" } }; - assertErrorCode(local, pipeline, 16410, "connectFromField cannot be a fieldPath"); +assertErrorCode(local, pipeline, 16410, "connectFromField cannot be a fieldPath"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -128,9 +127,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". as: "output" } }; - assertErrorCode(local, pipeline, 40103, "connectToField must be a string"); +assertErrorCode(local, pipeline, 40103, "connectToField must be a string"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -139,9 +138,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". as: "output" } }; - assertErrorCode(local, pipeline, 16410, "connectToField cannot be a fieldPath"); +assertErrorCode(local, pipeline, 16410, "connectToField cannot be a fieldPath"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -151,9 +150,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". depthField: 0 } }; - assertErrorCode(local, pipeline, 40103, "depthField must be a string"); +assertErrorCode(local, pipeline, 40103, "depthField must be a string"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -163,9 +162,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". depthField: "$depth" } }; - assertErrorCode(local, pipeline, 16410, "depthField cannot be a fieldPath"); +assertErrorCode(local, pipeline, 16410, "depthField cannot be a fieldPath"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -175,9 +174,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". restrictSearchWithMatch: "notamatch" } }; - assertErrorCode(local, pipeline, 40185, "restrictSearchWithMatch must be an object"); +assertErrorCode(local, pipeline, 40185, "restrictSearchWithMatch must be an object"); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -187,43 +186,37 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". notAField: "foo" } }; - assertErrorCode(local, pipeline, 40104, "unknown argument"); - - pipeline = { - $graphLookup: - {from: "foreign", startWith: {$literal: 0}, connectFromField: "b", as: "output"} - }; - assertErrorCode(local, pipeline, 40105, "connectToField was not specified"); - - pipeline = { - $graphLookup: - {from: "foreign", startWith: {$literal: 0}, connectToField: "a", as: "output"} - }; - assertErrorCode(local, pipeline, 40105, "connectFromField was not specified"); - - pipeline = { - $graphLookup: {from: "foreign", connectToField: "a", connectFromField: "b", as: "output"} - }; - assertErrorCode(local, pipeline, 40105, "startWith was not specified"); - - pipeline = { - $graphLookup: { - from: "foreign", - startWith: {$literal: 0}, - connectToField: "a", - connectFromField: "b" - } - }; - assertErrorCode(local, pipeline, 40105, "as was not specified"); - - pipeline = { - $graphLookup: - {startWith: {$literal: 0}, connectToField: "a", connectFromField: "b", as: "output"} - }; - assertErrorCode(local, pipeline, ErrorCodes.FailedToParse, "from was not specified"); - - // restrictSearchWithMatch must be a valid match expression. - pipeline = { +assertErrorCode(local, pipeline, 40104, "unknown argument"); + +pipeline = { + $graphLookup: {from: "foreign", startWith: {$literal: 0}, connectFromField: "b", as: "output"} +}; +assertErrorCode(local, pipeline, 40105, "connectToField was not specified"); + +pipeline = { + $graphLookup: {from: "foreign", startWith: {$literal: 0}, connectToField: "a", as: "output"} +}; +assertErrorCode(local, pipeline, 40105, "connectFromField was not specified"); + +pipeline = { + $graphLookup: {from: "foreign", connectToField: "a", connectFromField: "b", as: "output"} +}; +assertErrorCode(local, pipeline, 40105, "startWith was not specified"); + +pipeline = { + $graphLookup: + {from: "foreign", startWith: {$literal: 0}, connectToField: "a", connectFromField: "b"} +}; +assertErrorCode(local, pipeline, 40105, "as was not specified"); + +pipeline = { + $graphLookup: + {startWith: {$literal: 0}, connectToField: "a", connectFromField: "b", as: "output"} +}; +assertErrorCode(local, pipeline, ErrorCodes.FailedToParse, "from was not specified"); + +// restrictSearchWithMatch must be a valid match expression. +pipeline = { $graphLookup: { from: 'foreign', startWith: {$literal: 0}, @@ -233,10 +226,10 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". restrictSearchWithMatch: {$not: {a: 1}} } }; - assert.throws(() => local.aggregate(pipeline), [], "unable to parse match expression"); +assert.throws(() => local.aggregate(pipeline), [], "unable to parse match expression"); - // $where and $text cannot be used inside $graphLookup. - pipeline = { +// $where and $text cannot be used inside $graphLookup. +pipeline = { $graphLookup: { from: 'foreign', startWith: {$literal: 0}, @@ -246,9 +239,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". restrictSearchWithMatch: {$where: "3 > 2"} } }; - assert.throws(() => local.aggregate(pipeline), [], "cannot use $where inside $graphLookup"); +assert.throws(() => local.aggregate(pipeline), [], "cannot use $where inside $graphLookup"); - pipeline = { +pipeline = { $graphLookup: { from: 'foreign', startWith: {$literal: 0}, @@ -258,9 +251,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". restrictSearchWithMatch: {$text: {$search: "some text"}} } }; - assert.throws(() => local.aggregate(pipeline), [], "cannot use $text inside $graphLookup"); +assert.throws(() => local.aggregate(pipeline), [], "cannot use $text inside $graphLookup"); - pipeline = { +pipeline = { $graphLookup: { from: 'foreign', startWith: {$literal: 0}, @@ -272,9 +265,9 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". } } }; - assert.throws(() => local.aggregate(pipeline), [], "cannot use $near inside $graphLookup"); +assert.throws(() => local.aggregate(pipeline), [], "cannot use $near inside $graphLookup"); - pipeline = { +pipeline = { $graphLookup: { from: 'foreign', startWith: {$literal: 0}, @@ -293,15 +286,15 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". } } }; - assert.throws( - () => local.aggregate(pipeline), [], "cannot use $near inside $graphLookup at any depth"); +assert.throws( + () => local.aggregate(pipeline), [], "cannot use $near inside $graphLookup at any depth"); - let foreign = db.foreign; - foreign.drop(); - assert.writeOK(foreign.insert({a: 0, x: 0})); +let foreign = db.foreign; +foreign.drop(); +assert.writeOK(foreign.insert({a: 0, x: 0})); - // Test a restrictSearchWithMatch expression that fails to parse. - pipeline = { +// Test a restrictSearchWithMatch expression that fails to parse. +pipeline = { $graphLookup: { from: 'foreign', startWith: {$literal: 0}, @@ -311,10 +304,10 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". restrictSearchWithMatch: {$expr: {$eq: ["$x", "$$unbound"]}} } }; - assert.throws(() => local.aggregate(pipeline), [], "cannot use $expr with unbound variable"); +assert.throws(() => local.aggregate(pipeline), [], "cannot use $expr with unbound variable"); - // Test a restrictSearchWithMatchExpression that throws at runtime. - pipeline = { +// Test a restrictSearchWithMatchExpression that throws at runtime. +pipeline = { $graphLookup: { from: 'foreign', startWith: {$literal: 0}, @@ -324,25 +317,25 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". restrictSearchWithMatch: {$expr: {$divide: [1, "$x"]}} } }; - assertErrorCode(local, pipeline, 16608, "division by zero in $expr"); +assertErrorCode(local, pipeline, 16608, "division by zero in $expr"); - // $graphLookup can only consume at most 100MB of memory. - foreign.drop(); +// $graphLookup can only consume at most 100MB of memory. +foreign.drop(); - // Here, the visited set exceeds 100MB. - var bulk = foreign.initializeUnorderedBulkOp(); +// Here, the visited set exceeds 100MB. +var bulk = foreign.initializeUnorderedBulkOp(); - var initial = []; - for (var i = 0; i < 8; i++) { - var obj = {_id: i}; +var initial = []; +for (var i = 0; i < 8; i++) { + var obj = {_id: i}; - obj['longString'] = new Array(14 * 1024 * 1024).join('x'); - initial.push(i); - bulk.insert(obj); - } - assert.writeOK(bulk.execute()); + obj['longString'] = new Array(14 * 1024 * 1024).join('x'); + initial.push(i); + bulk.insert(obj); +} +assert.writeOK(bulk.execute()); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: initial}, @@ -351,21 +344,21 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". as: "graph" } }; - assertErrorCode(local, pipeline, 40099, "maximum memory usage reached"); +assertErrorCode(local, pipeline, 40099, "maximum memory usage reached"); - // Here, the visited set should grow to approximately 90 MB, and the frontier should push memory - // usage over 100MB. - foreign.drop(); +// Here, the visited set should grow to approximately 90 MB, and the frontier should push memory +// usage over 100MB. +foreign.drop(); - var bulk = foreign.initializeUnorderedBulkOp(); - for (var i = 0; i < 14; i++) { - var obj = {from: 0, to: 1}; - obj['s'] = new Array(7 * 1024 * 1024).join(' '); - bulk.insert(obj); - } - assert.writeOK(bulk.execute()); +var bulk = foreign.initializeUnorderedBulkOp(); +for (var i = 0; i < 14; i++) { + var obj = {from: 0, to: 1}; + obj['s'] = new Array(7 * 1024 * 1024).join(' '); + bulk.insert(obj); +} +assert.writeOK(bulk.execute()); - pipeline = { +pipeline = { $graphLookup: { from: "foreign", startWith: {$literal: 0}, @@ -375,20 +368,20 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". } }; - assertErrorCode(local, pipeline, 40099, "maximum memory usage reached"); +assertErrorCode(local, pipeline, 40099, "maximum memory usage reached"); - // Here, we test that the cache keeps memory usage under 100MB, and does not cause an error. - foreign.drop(); +// Here, we test that the cache keeps memory usage under 100MB, and does not cause an error. +foreign.drop(); - var bulk = foreign.initializeUnorderedBulkOp(); - for (var i = 0; i < 13; i++) { - var obj = {from: 0, to: 1}; - obj['s'] = new Array(7 * 1024 * 1024).join(' '); - bulk.insert(obj); - } - assert.writeOK(bulk.execute()); +var bulk = foreign.initializeUnorderedBulkOp(); +for (var i = 0; i < 13; i++) { + var obj = {from: 0, to: 1}; + obj['s'] = new Array(7 * 1024 * 1024).join(' '); + bulk.insert(obj); +} +assert.writeOK(bulk.execute()); - var res = local +var res = local .aggregate({ $graphLookup: { from: "foreign", @@ -401,5 +394,5 @@ load("jstests/aggregation/extras/utils.js"); // For "assertErrorCode". {$unwind: {path: "$out"}}) .toArray(); - assert.eq(res.length, 13); +assert.eq(res.length, 13); }()); diff --git a/jstests/aggregation/sources/graphLookup/filter.js b/jstests/aggregation/sources/graphLookup/filter.js index 69027500aae..4b46c843d9a 100644 --- a/jstests/aggregation/sources/graphLookup/filter.js +++ b/jstests/aggregation/sources/graphLookup/filter.js @@ -6,23 +6,23 @@ // we test the functionality and correctness of the option. (function() { - "use strict"; +"use strict"; - var local = db.local; - var foreign = db.foreign; +var local = db.local; +var foreign = db.foreign; - local.drop(); - foreign.drop(); +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})); +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 +// Assert that the graphLookup only retrieves ten documents, with _id from 0 to 9. +var res = local .aggregate({ $graphLookup: { from: "foreign", @@ -35,11 +35,11 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 10); +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 +// 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", @@ -52,16 +52,16 @@ }) .toArray()[0]; - assert.eq(res.integers.length, 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})); +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 +// Assert that the $graphLookup stops exploring when it finds a document that doesn't match the +// filter. +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -74,10 +74,10 @@ }) .toArray()[0]; - assert.eq(res.results.length, 1); +assert.eq(res.results.length, 1); - // $expr is allowed inside the 'restrictSearchWithMatch' match expression. - res = local +// $expr is allowed inside the 'restrictSearchWithMatch' match expression. +res = local .aggregate({ $graphLookup: { from: "foreign", @@ -90,5 +90,5 @@ }) .toArray()[0]; - assert.eq(res.results.length, 1); +assert.eq(res.results.length, 1); })(); diff --git a/jstests/aggregation/sources/graphLookup/nested_objects.js b/jstests/aggregation/sources/graphLookup/nested_objects.js index d40cced2ac4..43c81302ae4 100644 --- a/jstests/aggregation/sources/graphLookup/nested_objects.js +++ b/jstests/aggregation/sources/graphLookup/nested_objects.js @@ -6,24 +6,24 @@ // when the 'connectToField' is a nested array, or when the 'connectFromField' is a nested array. (function() { - "use strict"; +"use strict"; - var local = db.local; - var foreign = db.foreign; +var local = db.local; +var foreign = db.foreign; - local.drop(); - foreign.drop(); +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.writeOK(bulk.execute()); +// '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.writeOK(bulk.execute()); - assert.writeOK(local.insert({starting: 0})); +assert.writeOK(local.insert({starting: 0})); - var res = local +var res = local .aggregate({ $graphLookup: { from: "foreign", @@ -34,18 +34,18 @@ } }) .toArray()[0]; - assert.eq(res.integers.length, 100); +assert.eq(res.integers.length, 100); - foreign.drop(); +foreign.drop(); - // 'connectToField' is an array of objects. - var bulk = foreign.initializeUnorderedBulkOp(); - for (var i = 0; i < 100; i++) { - bulk.insert({previous: [{neighbor: i}, {neighbor: i - 1}], value: i + 1}); - } - assert.writeOK(bulk.execute()); +// 'connectToField' is an array of objects. +var bulk = foreign.initializeUnorderedBulkOp(); +for (var i = 0; i < 100; i++) { + bulk.insert({previous: [{neighbor: i}, {neighbor: i - 1}], value: i + 1}); +} +assert.writeOK(bulk.execute()); - var res = local +var res = local .aggregate({ $graphLookup: { from: "foreign", @@ -56,21 +56,21 @@ } }) .toArray()[0]; - assert.eq(res.integers.length, 100); +assert.eq(res.integers.length, 100); - foreign.drop(); +foreign.drop(); - // Both 'connectToField' and 'connectFromField' are arrays of objects. - var bulk = foreign.initializeUnorderedBulkOp(); - for (var i = 0; i < 100; i++) { - bulk.insert({ - previous: [{neighbor: i}, {neighbor: i - 1}], - values: [{neighbor: i + 1}, {neighbor: i + 2}] - }); - } - assert.writeOK(bulk.execute()); +// Both 'connectToField' and 'connectFromField' are arrays of objects. +var bulk = foreign.initializeUnorderedBulkOp(); +for (var i = 0; i < 100; i++) { + bulk.insert({ + previous: [{neighbor: i}, {neighbor: i - 1}], + values: [{neighbor: i + 1}, {neighbor: i + 2}] + }); +} +assert.writeOK(bulk.execute()); - var res = local +var res = local .aggregate({ $graphLookup: { from: "foreign", @@ -81,5 +81,5 @@ } }) .toArray()[0]; - assert.eq(res.integers.length, 100); +assert.eq(res.integers.length, 100); }()); diff --git a/jstests/aggregation/sources/graphLookup/socialite.js b/jstests/aggregation/sources/graphLookup/socialite.js index 228c0f56c0e..f38f6c2ffc0 100644 --- a/jstests/aggregation/sources/graphLookup/socialite.js +++ b/jstests/aggregation/sources/graphLookup/socialite.js @@ -6,35 +6,35 @@ // Socialite schema example available here: https://github.com/mongodb-labs/socialite (function() { - "use strict"; +"use strict"; - var follower = db.followers; - var users = db.users; +var follower = db.followers; +var users = db.users; - follower.drop(); - users.drop(); +follower.drop(); +users.drop(); - var userDocs = [ - {_id: "djw", fullname: "Darren", country: "Australia"}, - {_id: "bmw", fullname: "Bob", country: "Germany"}, - {_id: "jsr", fullname: "Jared", country: "USA"}, - {_id: "ftr", fullname: "Frank", country: "Canada"} - ]; +var userDocs = [ + {_id: "djw", fullname: "Darren", country: "Australia"}, + {_id: "bmw", fullname: "Bob", country: "Germany"}, + {_id: "jsr", fullname: "Jared", country: "USA"}, + {_id: "ftr", fullname: "Frank", country: "Canada"} +]; - userDocs.forEach(function(userDoc) { - assert.writeOK(users.insert(userDoc)); - }); +userDocs.forEach(function(userDoc) { + assert.writeOK(users.insert(userDoc)); +}); - var followers = [{_f: "djw", _t: "jsr"}, {_f: "jsr", _t: "bmw"}, {_f: "ftr", _t: "bmw"}]; +var followers = [{_f: "djw", _t: "jsr"}, {_f: "jsr", _t: "bmw"}, {_f: "ftr", _t: "bmw"}]; - followers.forEach(function(f) { - assert.writeOK(follower.insert(f)); - }); +followers.forEach(function(f) { + assert.writeOK(follower.insert(f)); +}); - // Find the social network of "Darren", that is, people Darren follows, and people who are - // followed by someone Darren follows, etc. +// Find the social network of "Darren", that is, people Darren follows, and people who are +// followed by someone Darren follows, etc. - var res = users +var res = users .aggregate({$match: {fullname: "Darren"}}, { $graphLookup: { @@ -49,6 +49,6 @@ {$project: {_id: "$network._t"}}) .toArray(); - // "djw" is followed, directly or indirectly, by "jsr" and "bmw". - assert.eq(res.length, 2); +// "djw" is followed, directly or indirectly, by "jsr" and "bmw". +assert.eq(res.length, 2); }()); diff --git a/jstests/aggregation/sources/graphLookup/variables.js b/jstests/aggregation/sources/graphLookup/variables.js index 87e2c8b3975..63b1bbea244 100644 --- a/jstests/aggregation/sources/graphLookup/variables.js +++ b/jstests/aggregation/sources/graphLookup/variables.js @@ -2,17 +2,17 @@ * Tests to verify that $graphLookup can use the variables defined in an outer scope. */ (function() { - "use strict"; +"use strict"; - let local = db.graph_lookup_var_local; - let foreign = db.graph_lookup_var_foreign; - local.drop(); - foreign.drop(); +let local = db.graph_lookup_var_local; +let foreign = db.graph_lookup_var_foreign; +local.drop(); +foreign.drop(); - foreign.insert({from: "b", to: "a", _id: 0}); - local.insert({}); +foreign.insert({from: "b", to: "a", _id: 0}); +local.insert({}); - const basicGraphLookup = { +const basicGraphLookup = { $graphLookup: { from: "graph_lookup_var_foreign", startWith: "$$var1", @@ -22,7 +22,7 @@ } }; - const lookup = { +const lookup = { $lookup: { from: "graph_lookup_var_local", let : {var1: "a"}, @@ -31,11 +31,10 @@ } }; - // Verify that $graphLookup can use the variable 'var1' which is defined in parent $lookup. - let res = local.aggregate([lookup]).toArray(); - assert.eq(res.length, 1); - assert.eq(res[0].resultsFromLookup.length, 1); - assert.eq(res[0].resultsFromLookup[0].resultsFromGraphLookup.length, 1); - assert.eq(res[0].resultsFromLookup[0].resultsFromGraphLookup[0], {_id: 0, from: "b", to: "a"}); - +// Verify that $graphLookup can use the variable 'var1' which is defined in parent $lookup. +let res = local.aggregate([lookup]).toArray(); +assert.eq(res.length, 1); +assert.eq(res[0].resultsFromLookup.length, 1); +assert.eq(res[0].resultsFromLookup[0].resultsFromGraphLookup.length, 1); +assert.eq(res[0].resultsFromLookup[0].resultsFromGraphLookup[0], {_id: 0, from: "b", to: "a"}); })(); |