diff options
Diffstat (limited to 'jstests/aggregation/expressions')
-rw-r--r-- | jstests/aggregation/expressions/expression_mod.js | 79 | ||||
-rw-r--r-- | jstests/aggregation/expressions/in.js | 46 | ||||
-rw-r--r-- | jstests/aggregation/expressions/indexof_array.js | 16 | ||||
-rw-r--r-- | jstests/aggregation/expressions/indexof_bytes.js | 20 | ||||
-rw-r--r-- | jstests/aggregation/expressions/indexof_codepoints.js | 20 | ||||
-rw-r--r-- | jstests/aggregation/expressions/reduce.js | 90 | ||||
-rw-r--r-- | jstests/aggregation/expressions/split.js | 24 | ||||
-rw-r--r-- | jstests/aggregation/expressions/switch.js | 6 | ||||
-rw-r--r-- | jstests/aggregation/expressions/switch_errors.js | 28 |
9 files changed, 120 insertions, 209 deletions
diff --git a/jstests/aggregation/expressions/expression_mod.js b/jstests/aggregation/expressions/expression_mod.js index 923324797ed..63469ca8177 100644 --- a/jstests/aggregation/expressions/expression_mod.js +++ b/jstests/aggregation/expressions/expression_mod.js @@ -17,47 +17,44 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp // The $match portion ensures they are of the correct type as the shell turns the ints back to // doubles at the end so we can not check types with assert. coll.save({}); - var result = - coll.aggregate( - { - $project: { - _id: 0, - dub_dub: {$mod: [138.5, 3.0]}, - dub_int: {$mod: [138.5, NumberLong(3)]}, - dub_long: {$mod: [138.5, NumberInt(3)]}, - int_dub: {$mod: [NumberInt(8), 3.25]}, - int_dubint: {$mod: [NumberInt(8), 3.0]}, - int_int: {$mod: [NumberInt(8), NumberInt(3)]}, - int_long: {$mod: [NumberInt(8), NumberLong(3)]}, - long_dub: {$mod: [NumberLong(8), 3.25]}, - long_dubint: {$mod: [NumberLong(8), 3.0]}, - long_dublong: {$mod: [NumberLong(500000000000), 450000000000.0]}, - long_int: {$mod: [NumberLong(8), NumberInt(3)]}, - long_long: {$mod: [NumberLong(8), NumberLong(3)]}, - verylong_verylong: - {$mod: [NumberLong(800000000000), NumberLong(300000000000)]} - } - }, - { - $match: { - // 1 is NumberDouble - dub_dub: {$type: 1}, - dub_int: {$type: 1}, - dub_long: {$type: 1}, - int_dub: {$type: 1}, - // 16 is NumberInt - int_dubint: {$type: 16}, - int_int: {$type: 16}, - // 18 is NumberLong - int_long: {$type: 18}, - long_dub: {$type: 1}, - long_dubint: {$type: 18}, - long_dublong: {$type: 1}, - long_int: {$type: 18}, - long_long: {$type: 18}, - verylong_verylong: {$type: 18} - } - }); + var result = coll.aggregate({ + $project: { + _id: 0, + dub_dub: {$mod: [138.5, 3.0]}, + dub_int: {$mod: [138.5, NumberLong(3)]}, + dub_long: {$mod: [138.5, NumberInt(3)]}, + int_dub: {$mod: [NumberInt(8), 3.25]}, + int_dubint: {$mod: [NumberInt(8), 3.0]}, + int_int: {$mod: [NumberInt(8), NumberInt(3)]}, + int_long: {$mod: [NumberInt(8), NumberLong(3)]}, + long_dub: {$mod: [NumberLong(8), 3.25]}, + long_dubint: {$mod: [NumberLong(8), 3.0]}, + long_dublong: {$mod: [NumberLong(500000000000), 450000000000.0]}, + long_int: {$mod: [NumberLong(8), NumberInt(3)]}, + long_long: {$mod: [NumberLong(8), NumberLong(3)]}, + verylong_verylong: {$mod: [NumberLong(800000000000), NumberLong(300000000000)]} + } + }, + { + $match: { + // 1 is NumberDouble + dub_dub: {$type: 1}, + dub_int: {$type: 1}, + dub_long: {$type: 1}, + int_dub: {$type: 1}, + // 16 is NumberInt + int_dubint: {$type: 16}, + int_int: {$type: 16}, + // 18 is NumberLong + int_long: {$type: 18}, + long_dub: {$type: 1}, + long_dubint: {$type: 18}, + long_dublong: {$type: 1}, + long_int: {$type: 18}, + long_long: {$type: 18}, + verylong_verylong: {$type: 18} + } + }); // Correct answers (it is mainly the types that are important here). var expectedResult = [{ diff --git a/jstests/aggregation/expressions/in.js b/jstests/aggregation/expressions/in.js index 7839c95d32e..ba09b0f8fac 100644 --- a/jstests/aggregation/expressions/in.js +++ b/jstests/aggregation/expressions/in.js @@ -5,13 +5,11 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode. (function() { "use strict"; - var coll = db.in; + var coll = db.in ; coll.drop(); function testExpression(options) { - var pipeline = { - $project: {included: {$in: ["$elementField", {$literal: options.array}]}} - }; + var pipeline = {$project: {included: {$in: ["$elementField", {$literal: options.array}]}}}; coll.drop(); assert.writeOK(coll.insert({elementField: options.element})); @@ -20,9 +18,7 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode. assert.eq(res[0].included, options.elementIsIncluded); if (options.queryFormShouldBeEquivalent) { - var query = { - elementField: {$in: options.array} - }; + var query = {elementField: {$in: options.array}}; res = coll.find(query).toArray(); if (options.elementIsIncluded) { @@ -33,12 +29,8 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode. } } - testExpression({ - element: 1, - array: [1, 2, 3], - elementIsIncluded: true, - queryFormShouldBeEquivalent: true - }); + testExpression( + {element: 1, array: [1, 2, 3], elementIsIncluded: true, queryFormShouldBeEquivalent: true}); testExpression({ element: "A", @@ -119,38 +111,24 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode. coll.drop(); coll.insert({}); - var pipeline = { - $project: {included: {$in: [[1, 2], 1]}} - }; + var pipeline = {$project: {included: {$in: [[1, 2], 1]}}}; assertErrorCode(coll, pipeline, 40081, "$in requires an array as a second argument"); - pipeline = { - $project: {included: {$in: [1, null]}} - }; + pipeline = {$project: {included: {$in: [1, null]}}}; assertErrorCode(coll, pipeline, 40081, "$in requires an array as a second argument"); - pipeline = { - $project: {included: {$in: [1, "$notAField"]}} - }; + pipeline = {$project: {included: {$in: [1, "$notAField"]}}}; assertErrorCode(coll, pipeline, 40081, "$in requires an array as a second argument"); - pipeline = { - $project: {included: {$in: null}} - }; + pipeline = {$project: {included: {$in: null}}}; assertErrorCode(coll, pipeline, 16020, "$in requires two arguments"); - pipeline = { - $project: {included: {$in: [1]}} - }; + pipeline = {$project: {included: {$in: [1]}}}; assertErrorCode(coll, pipeline, 16020, "$in requires two arguments"); - pipeline = { - $project: {included: {$in: []}} - }; + pipeline = {$project: {included: {$in: []}}}; assertErrorCode(coll, pipeline, 16020, "$in requires two arguments"); - pipeline = { - $project: {included: {$in: [1, 2, 3]}} - }; + pipeline = {$project: {included: {$in: [1, 2, 3]}}}; assertErrorCode(coll, pipeline, 16020, "$in requires two arguments"); }()); diff --git a/jstests/aggregation/expressions/indexof_array.js b/jstests/aggregation/expressions/indexof_array.js index bfc9ef71a15..3fb445e5066 100644 --- a/jstests/aggregation/expressions/indexof_array.js +++ b/jstests/aggregation/expressions/indexof_array.js @@ -46,23 +46,15 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp }; assertErrorCode(coll, pipeline, 40090); - pipeline = { - $project: {output: {$indexOfArray: [[1, 2, 3], 2, "bad"]}} - }; + pipeline = {$project: {output: {$indexOfArray: [[1, 2, 3], 2, "bad"]}}}; assertErrorCode(coll, pipeline, 40096); - pipeline = { - $project: {output: {$indexOfArray: [[1, 2, 3], 2, 0, "bad"]}} - }; + pipeline = {$project: {output: {$indexOfArray: [[1, 2, 3], 2, 0, "bad"]}}}; assertErrorCode(coll, pipeline, 40096); - pipeline = { - $project: {output: {$indexOfArray: [[1, 2, 3], 2, -1]}} - }; + pipeline = {$project: {output: {$indexOfArray: [[1, 2, 3], 2, -1]}}}; assertErrorCode(coll, pipeline, 40097); - pipeline = { - $project: {output: {$indexOfArray: [[1, 2, 3], 2, 1, -1]}} - }; + pipeline = {$project: {output: {$indexOfArray: [[1, 2, 3], 2, 1, -1]}}}; assertErrorCode(coll, pipeline, 40097); }()); diff --git a/jstests/aggregation/expressions/indexof_bytes.js b/jstests/aggregation/expressions/indexof_bytes.js index ac3cefda790..d484ad50948 100644 --- a/jstests/aggregation/expressions/indexof_bytes.js +++ b/jstests/aggregation/expressions/indexof_bytes.js @@ -17,9 +17,7 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp var start = indexOfSpec.length > 2 ? indexOfSpec[2] : 0; // Use $strLenBytes because JavaScript's length property is based off of UTF-16, not the // actual number of bytes. - var end = indexOfSpec.length > 3 ? indexOfSpec[3] : { - $strLenBytes: input - }; + var end = indexOfSpec.length > 3 ? indexOfSpec[3] : {$strLenBytes: input}; var substrExpr = { $indexOfBytes: [{$substrBytes: [input, start, {$subtract: [end, start]}]}, token] @@ -127,23 +125,15 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp }; assertErrorCode(coll, pipeline, 40092); - pipeline = { - $project: {output: {$indexOfBytes: ["abc", "b", "bad"]}} - }; + pipeline = {$project: {output: {$indexOfBytes: ["abc", "b", "bad"]}}}; assertErrorCode(coll, pipeline, 40096); - pipeline = { - $project: {output: {$indexOfBytes: ["abc", "b", 0, "bad"]}} - }; + pipeline = {$project: {output: {$indexOfBytes: ["abc", "b", 0, "bad"]}}}; assertErrorCode(coll, pipeline, 40096); - pipeline = { - $project: {output: {$indexOfBytes: ["abc", "b", -1]}} - }; + pipeline = {$project: {output: {$indexOfBytes: ["abc", "b", -1]}}}; assertErrorCode(coll, pipeline, 40097); - pipeline = { - $project: {output: {$indexOfBytes: ["abc", "b", 1, -1]}} - }; + pipeline = {$project: {output: {$indexOfBytes: ["abc", "b", 1, -1]}}}; assertErrorCode(coll, pipeline, 40097); }()); diff --git a/jstests/aggregation/expressions/indexof_codepoints.js b/jstests/aggregation/expressions/indexof_codepoints.js index 20b9534b050..506b1a13cfa 100644 --- a/jstests/aggregation/expressions/indexof_codepoints.js +++ b/jstests/aggregation/expressions/indexof_codepoints.js @@ -15,9 +15,7 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp var input = indexOfSpec[0]; var token = indexOfSpec[1]; var start = indexOfSpec.length > 2 ? indexOfSpec[2] : 0; - var end = indexOfSpec.length > 3 ? indexOfSpec[3] : { - $strLenCP: input - }; + var end = indexOfSpec.length > 3 ? indexOfSpec[3] : {$strLenCP: input}; var substrExpr = { $indexOfCP: [{$substrCP: [input, start, {$subtract: [end, start]}]}, token] @@ -107,23 +105,15 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp }; assertErrorCode(coll, pipeline, 40094); - pipeline = { - $project: {output: {$indexOfCP: ["abc", "b", "bad"]}} - }; + pipeline = {$project: {output: {$indexOfCP: ["abc", "b", "bad"]}}}; assertErrorCode(coll, pipeline, 40096); - pipeline = { - $project: {output: {$indexOfCP: ["abc", "b", 0, "bad"]}} - }; + pipeline = {$project: {output: {$indexOfCP: ["abc", "b", 0, "bad"]}}}; assertErrorCode(coll, pipeline, 40096); - pipeline = { - $project: {output: {$indexOfCP: ["abc", "b", -1]}} - }; + pipeline = {$project: {output: {$indexOfCP: ["abc", "b", -1]}}}; assertErrorCode(coll, pipeline, 40097); - pipeline = { - $project: {output: {$indexOfCP: ["abc", "b", 1, -1]}} - }; + pipeline = {$project: {output: {$indexOfCP: ["abc", "b", 1, -1]}}}; assertErrorCode(coll, pipeline, 40097); }()); diff --git a/jstests/aggregation/expressions/reduce.js b/jstests/aggregation/expressions/reduce.js index 73bb0cfa16f..54a66fc8b56 100644 --- a/jstests/aggregation/expressions/reduce.js +++ b/jstests/aggregation/expressions/reduce.js @@ -11,15 +11,15 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp coll, { $reduce: - {input: [1, 2, 3], initialValue: {$literal: 0}, in: {$sum: ["$$this", "$$value"]}} + {input: [1, 2, 3], initialValue: {$literal: 0}, in : {$sum: ["$$this", "$$value"]}} }, 6); - testExpression(coll, {$reduce: {input: [], initialValue: {$literal: 0}, in: 10}}, 0); + testExpression(coll, {$reduce: {input: [], initialValue: {$literal: 0}, in : 10}}, 0); testExpression( coll, { $reduce: - {input: [1, 2, 3], initialValue: [], in: {$concatArrays: ["$$value", ["$$this"]]}} + {input: [1, 2, 3], initialValue: [], in : {$concatArrays: ["$$value", ["$$this"]]}} }, [1, 2, 3]); testExpression(coll, @@ -27,7 +27,7 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp $reduce: { input: [1, 2], initialValue: [], - in: {$concatArrays: ["$$value", ["$$value.notAField"]]} + in : {$concatArrays: ["$$value", ["$$value.notAField"]]} } }, [[], []]); @@ -38,14 +38,14 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp $reduce: { input: [[1, 2, 3], [4, 5]], initialValue: 1, - in: { + in : { $multiply: [ "$$value", { $reduce: { input: "$$this", initialValue: 0, - in: {$sum: ["$$value", "$$this"]} + in : {$sum: ["$$value", "$$this"]} } } ] @@ -56,43 +56,43 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp // A nested $reduce using a $let to allow the inner $reduce to access the variables of the // outer. - testExpression( - coll, - { - $reduce: { - input: [[0, 1], [2, 3]], - initialValue: {allElements: [], sumOfInner: {$literal: 0}}, - in: { - $let: { - vars: {outerValue: "$$value", innerArray: "$$this"}, - in: { - $reduce: { - input: "$$innerArray", - initialValue: "$$outerValue", - in: { - allElements: - {$concatArrays: ["$$value.allElements", ["$$this"]]}, - sumOfInner: {$sum: ["$$value.sumOfInner", "$$this"]} - } - } - } - } - } - } - }, - {allElements: [0, 1, 2, 3], sumOfInner: 6}); + testExpression(coll, + { + $reduce: { + input: [[0, 1], [2, 3]], + initialValue: {allElements: [], sumOfInner: {$literal: 0}}, + in : { + $let: { + vars: {outerValue: "$$value", innerArray: "$$this"}, + in : { + $reduce: { + input: "$$innerArray", + initialValue: "$$outerValue", + in : { + allElements: { + $concatArrays: + ["$$value.allElements", ["$$this"]] + }, + sumOfInner: + {$sum: ["$$value.sumOfInner", "$$this"]} + } + } + } + } + } + } + }, + {allElements: [0, 1, 2, 3], sumOfInner: 6}); // Nullish input produces null as an output. - testExpression(coll, {$reduce: {input: null, initialValue: {$literal: 0}, in: 5}}, null); + testExpression(coll, {$reduce: {input: null, initialValue: {$literal: 0}, in : 5}}, null); testExpression( - coll, {$reduce: {input: "$nonexistent", initialValue: {$literal: 0}, in: 5}}, null); + coll, {$reduce: {input: "$nonexistent", initialValue: {$literal: 0}, in : 5}}, null); // Error cases for $reduce. // $reduce requires an object. - var pipeline = { - $project: {reduced: {$reduce: 0}} - }; + var pipeline = {$project: {reduced: {$reduce: 0}}}; assertErrorCode(coll, pipeline, 40075); // Unknown field specified. @@ -102,7 +102,7 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp $reduce: { input: {$literal: 0}, initialValue: {$literal: 0}, - in: {$literal: 0}, + in : {$literal: 0}, notAField: {$literal: 0} } } @@ -111,15 +111,11 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp assertErrorCode(coll, pipeline, 40076); // $reduce requires input to be specified. - pipeline = { - $project: {reduced: {$reduce: {initialValue: {$literal: 0}, in: {$literal: 0}}}} - }; + pipeline = {$project: {reduced: {$reduce: {initialValue: {$literal: 0}, in : {$literal: 0}}}}}; assertErrorCode(coll, pipeline, 40077); // $reduce requires initialValue to be specified. - pipeline = { - $project: {reduced: {$reduce: {input: {$literal: 0}, in: {$literal: 0}}}} - }; + pipeline = {$project: {reduced: {$reduce: {input: {$literal: 0}, in : {$literal: 0}}}}}; assertErrorCode(coll, pipeline, 40078); // $reduce requires in to be specified. @@ -129,14 +125,10 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp assertErrorCode(coll, pipeline, 40079); // $$value is undefined in the non-'in' arguments of $reduce. - pipeline = { - $project: {reduced: {$reduce: {input: "$$value", initialValue: [], in: []}}} - }; + pipeline = {$project: {reduced: {$reduce: {input: "$$value", initialValue: [], in : []}}}}; assertErrorCode(coll, pipeline, 17276); // $$this is undefined in the non-'in' arguments of $reduce. - pipeline = { - $project: {reduced: {$reduce: {input: "$$this", initialValue: [], in: []}}} - }; + pipeline = {$project: {reduced: {$reduce: {input: "$$this", initialValue: [], in : []}}}}; assertErrorCode(coll, pipeline, 17276); }()); diff --git a/jstests/aggregation/expressions/split.js b/jstests/aggregation/expressions/split.js index bfed38314b4..7d3402bde4e 100644 --- a/jstests/aggregation/expressions/split.js +++ b/jstests/aggregation/expressions/split.js @@ -38,35 +38,23 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode and testExp testExpression(coll, {$split: ["a", "$a"]}, null); // Ensure that $split errors when given more or less than two arguments. - var pipeline = { - $project: {split: {$split: []}} - }; + var pipeline = {$project: {split: {$split: []}}}; assertErrorCode(coll, pipeline, 16020); - pipeline = { - $project: {split: {$split: ["a"]}} - }; + pipeline = {$project: {split: {$split: ["a"]}}}; assertErrorCode(coll, pipeline, 16020); - pipeline = { - $project: {split: {$split: ["a", "b", "c"]}} - }; + pipeline = {$project: {split: {$split: ["a", "b", "c"]}}}; assertErrorCode(coll, pipeline, 16020); // Ensure that $split errors when given non-string input. - pipeline = { - $project: {split: {$split: [1, "abc"]}} - }; + pipeline = {$project: {split: {$split: [1, "abc"]}}}; assertErrorCode(coll, pipeline, 40085); - pipeline = { - $project: {split: {$split: ["abc", 1]}} - }; + pipeline = {$project: {split: {$split: ["abc", 1]}}}; assertErrorCode(coll, pipeline, 40086); // Ensure that $split errors when given an empty separator. - pipeline = { - $project: {split: {$split: ["abc", ""]}} - }; + pipeline = {$project: {split: {$split: ["abc", ""]}}}; assertErrorCode(coll, pipeline, 40087); }()); diff --git a/jstests/aggregation/expressions/switch.js b/jstests/aggregation/expressions/switch.js index 9a6dbbb529d..64cd9e1db2f 100644 --- a/jstests/aggregation/expressions/switch.js +++ b/jstests/aggregation/expressions/switch.js @@ -99,8 +99,7 @@ pipeline = { "$project": { "_id": 0, - "output": - {"$switch": {"branches": [{"case": true, "then": null}], "default": false}} + "output": {"$switch": {"branches": [{"case": true, "then": null}], "default": false}} } }; res = coll.aggregate(pipeline).toArray(); @@ -125,8 +124,7 @@ pipeline = { "$project": { "_id": 0, - "output": - {"$switch": {"branches": [{"case": null, "then": false}], "default": null}} + "output": {"$switch": {"branches": [{"case": null, "then": false}], "default": null}} } }; res = coll.aggregate(pipeline).toArray(); diff --git a/jstests/aggregation/expressions/switch_errors.js b/jstests/aggregation/expressions/switch_errors.js index cf6dc0f4f93..0d9023fb250 100644 --- a/jstests/aggregation/expressions/switch_errors.js +++ b/jstests/aggregation/expressions/switch_errors.js @@ -8,24 +8,16 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode. var coll = db.switch; coll.drop(); - var pipeline = { - "$project": {"output": {"$switch": "not an object"}} - }; + var pipeline = {"$project": {"output": {"$switch": "not an object"}}}; assertErrorCode(coll, pipeline, 40060, "$switch requires an object as an argument."); - pipeline = { - "$project": {"output": {"$switch": {"branches": "not an array"}}} - }; + pipeline = {"$project": {"output": {"$switch": {"branches": "not an array"}}}}; assertErrorCode(coll, pipeline, 40061, "$switch requires 'branches' to be an array."); - pipeline = { - "$project": {"output": {"$switch": {"branches": ["not an object"]}}} - }; + pipeline = {"$project": {"output": {"$switch": {"branches": ["not an object"]}}}}; assertErrorCode(coll, pipeline, 40062, "$switch requires each branch to be an object."); - pipeline = { - "$project": {"output": {"$switch": {"branches": [{}]}}} - }; + pipeline = {"$project": {"output": {"$switch": {"branches": [{}]}}}}; assertErrorCode(coll, pipeline, 40064, "$switch requires each branch have a 'case'."); pipeline = { @@ -47,19 +39,13 @@ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode. }; assertErrorCode(coll, pipeline, 40063, "$switch found a branch with an unknown argument"); - pipeline = { - "$project": {"output": {"$switch": {"notAnArgument": 1}}} - }; + pipeline = {"$project": {"output": {"$switch": {"notAnArgument": 1}}}}; assertErrorCode(coll, pipeline, 40067, "$switch found an unknown argument"); - pipeline = { - "$project": {"output": {"$switch": {"branches": []}}} - }; + pipeline = {"$project": {"output": {"$switch": {"branches": []}}}}; assertErrorCode(coll, pipeline, 40068, "$switch requires at least one branch"); - pipeline = { - "$project": {"output": {"$switch": {}}} - }; + pipeline = {"$project": {"output": {"$switch": {}}}}; assertErrorCode(coll, pipeline, 40068, "$switch requires at least one branch"); coll.insert({x: 1}); |