diff options
Diffstat (limited to 'jstests/aggregation/sources/merge/merge_to_referenced_collection.js')
-rw-r--r-- | jstests/aggregation/sources/merge/merge_to_referenced_collection.js | 181 |
1 files changed, 76 insertions, 105 deletions
diff --git a/jstests/aggregation/sources/merge/merge_to_referenced_collection.js b/jstests/aggregation/sources/merge/merge_to_referenced_collection.js index c6a82bab79d..a9060f58b0a 100644 --- a/jstests/aggregation/sources/merge/merge_to_referenced_collection.js +++ b/jstests/aggregation/sources/merge/merge_to_referenced_collection.js @@ -9,64 +9,55 @@ * @tags: [assumes_unsharded_collection] */ (function() { - 'use strict'; +'use strict'; - load('jstests/aggregation/extras/merge_helpers.js'); // For 'withEachMergeMode'. - load('jstests/libs/fixture_helpers.js'); // For 'FixtureHelpers'. +load('jstests/aggregation/extras/merge_helpers.js'); // For 'withEachMergeMode'. +load('jstests/libs/fixture_helpers.js'); // For 'FixtureHelpers'. - const testDB = db.getSiblingDB("merge_to_referenced_coll"); - const coll = testDB.test; +const testDB = db.getSiblingDB("merge_to_referenced_coll"); +const coll = testDB.test; - withEachMergeMode(({whenMatchedMode, whenNotMatchedMode}) => { - coll.drop(); +withEachMergeMode(({whenMatchedMode, whenNotMatchedMode}) => { + coll.drop(); - // Seed the collection to ensure each pipeline will actually do something. - assert.commandWorked(coll.insert({_id: 0})); + // Seed the collection to ensure each pipeline will actually do something. + assert.commandWorked(coll.insert({_id: 0})); - // Each of the following assertions will somehow use $merge to write to a namespace that is - // being read from elsewhere in the pipeline. - const assertFailsWithCode = ((fn) => { - const error = assert.throws(fn); - assert.contains(error.code, [51188, 51079]); - }); + // Each of the following assertions will somehow use $merge to write to a namespace that is + // being read from elsewhere in the pipeline. + const assertFailsWithCode = ((fn) => { + const error = assert.throws(fn); + assert.contains(error.code, [51188, 51079]); + }); - // Test $merge to the aggregate command's source collection. - assertFailsWithCode(() => coll.aggregate([{ + // Test $merge to the aggregate command's source collection. + assertFailsWithCode(() => coll.aggregate([{ + $merge: + {into: coll.getName(), whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode} + }])); + + // Test $merge to the same namespace as a $lookup which is the same as the aggregate + // command's source collection. + assertFailsWithCode(() => coll.aggregate([ + {$lookup: {from: coll.getName(), as: "x", localField: "f_id", foreignField: "_id"}}, + { $merge: { into: coll.getName(), whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode } - }])); + } + ])); - // Test $merge to the same namespace as a $lookup which is the same as the aggregate - // command's source collection. - assertFailsWithCode(() => coll.aggregate([ - {$lookup: {from: coll.getName(), as: "x", localField: "f_id", foreignField: "_id"}}, - { - $merge: { - into: coll.getName(), - whenMatched: whenMatchedMode, - whenNotMatched: whenNotMatchedMode - } - } - ])); + // Test $merge to the same namespace as a $lookup which is *not* the same as the aggregate + // command's source collection. + assertFailsWithCode(() => coll.aggregate([ + {$lookup: {from: "bar", as: "x", localField: "f_id", foreignField: "_id"}}, + {$merge: {into: "bar", whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}} + ])); - // Test $merge to the same namespace as a $lookup which is *not* the same as the aggregate - // command's source collection. - assertFailsWithCode(() => coll.aggregate([ - {$lookup: {from: "bar", as: "x", localField: "f_id", foreignField: "_id"}}, - { - $merge: { - into: "bar", - whenMatched: whenMatchedMode, - whenNotMatched: whenNotMatchedMode - } - } - ])); - - // Test $merge to the same namespace as a $graphLookup. - assertFailsWithCode(() => coll.aggregate([ + // Test $merge to the same namespace as a $graphLookup. + assertFailsWithCode(() => coll.aggregate([ { $graphLookup: { from: "bar", @@ -85,8 +76,8 @@ } ])); - // Test $merge to the same namespace as a $lookup which is nested within another $lookup. - assertFailsWithCode(() => coll.aggregate([ + // Test $merge to the same namespace as a $lookup which is nested within another $lookup. + assertFailsWithCode(() => coll.aggregate([ { $lookup: { from: "bar", @@ -103,49 +94,33 @@ } } ])); - // Test $merge to the same namespace as a $lookup which is nested within a $facet. - assertFailsWithCode(() => coll.aggregate([ - { - $facet: { - y: [{$lookup: {from: "TARGET", as: "y", pipeline: []}}], - } - }, - { - $merge: { - into: "TARGET", - whenMatched: whenMatchedMode, - whenNotMatched: whenNotMatchedMode - } - } - ])); - assertFailsWithCode(() => coll.aggregate([ - { - $facet: { - x: [{$lookup: {from: "other", as: "y", pipeline: []}}], - y: [{$lookup: {from: "TARGET", as: "y", pipeline: []}}], - } - }, - { - $merge: { - into: "TARGET", - whenMatched: whenMatchedMode, - whenNotMatched: whenNotMatchedMode - } + // Test $merge to the same namespace as a $lookup which is nested within a $facet. + assertFailsWithCode(() => coll.aggregate([ + { + $facet: { + y: [{$lookup: {from: "TARGET", as: "y", pipeline: []}}], } - ])); - - // Test that we use the resolved namespace of a view to detect this sort of halloween - // problem. - assert.commandWorked( - testDB.runCommand({create: "view_on_TARGET", viewOn: "TARGET", pipeline: []})); - assertFailsWithCode(() => testDB.view_on_TARGET.aggregate([{ - $merge: { - into: "TARGET", - whenMatched: whenMatchedMode, - whenNotMatched: whenNotMatchedMode + }, + {$merge: {into: "TARGET", whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}} + ])); + assertFailsWithCode(() => coll.aggregate([ + { + $facet: { + x: [{$lookup: {from: "other", as: "y", pipeline: []}}], + y: [{$lookup: {from: "TARGET", as: "y", pipeline: []}}], } - }])); - assertFailsWithCode(() => coll.aggregate([ + }, + {$merge: {into: "TARGET", whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}} + ])); + + // Test that we use the resolved namespace of a view to detect this sort of halloween + // problem. + assert.commandWorked( + testDB.runCommand({create: "view_on_TARGET", viewOn: "TARGET", pipeline: []})); + assertFailsWithCode(() => testDB.view_on_TARGET.aggregate([ + {$merge: {into: "TARGET", whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}} + ])); + assertFailsWithCode(() => coll.aggregate([ { $facet: { x: [{$lookup: {from: "other", as: "y", pipeline: []}}], @@ -167,25 +142,21 @@ } ])); - function generateNestedPipeline(foreignCollName, numLevels) { - let pipeline = [{"$lookup": {pipeline: [], from: foreignCollName, as: "same"}}]; - - for (let level = 1; level < numLevels; level++) { - pipeline = [{"$lookup": {pipeline: pipeline, from: foreignCollName, as: "same"}}]; - } + function generateNestedPipeline(foreignCollName, numLevels) { + let pipeline = [{"$lookup": {pipeline: [], from: foreignCollName, as: "same"}}]; - return pipeline; + for (let level = 1; level < numLevels; level++) { + pipeline = [{"$lookup": {pipeline: pipeline, from: foreignCollName, as: "same"}}]; } - const nestedPipeline = generateNestedPipeline("lookup", 20).concat([{ - $merge: { - into: "lookup", - whenMatched: whenMatchedMode, - whenNotMatched: whenNotMatchedMode - } - }]); - assertFailsWithCode(() => coll.aggregate(nestedPipeline)); + return pipeline; + } - testDB.dropDatabase(); - }); + const nestedPipeline = generateNestedPipeline("lookup", 20).concat([ + {$merge: {into: "lookup", whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}} + ]); + assertFailsWithCode(() => coll.aggregate(nestedPipeline)); + + testDB.dropDatabase(); +}); }()); |