summaryrefslogtreecommitdiff
path: root/jstests/aggregation
diff options
context:
space:
mode:
authorDavid Storch <david.storch@10gen.com>2017-03-07 14:32:07 -0500
committerDavid Storch <david.storch@10gen.com>2017-03-13 17:50:16 -0400
commit2f367a1e6aafa0e6bdf58f0564d3dcd7b279733f (patch)
treeaed6dda9a3972b4723ea8091a5c5272a010cdff5 /jstests/aggregation
parenta0516b5f896703682c98cf0b8c2e333f743f4dc1 (diff)
downloadmongo-2f367a1e6aafa0e6bdf58f0564d3dcd7b279733f.tar.gz
SERVER-27614 add $$REMOVE agg system variable
Diffstat (limited to 'jstests/aggregation')
-rw-r--r--jstests/aggregation/variables/remove_system_variable.js87
1 files changed, 87 insertions, 0 deletions
diff --git a/jstests/aggregation/variables/remove_system_variable.js b/jstests/aggregation/variables/remove_system_variable.js
new file mode 100644
index 00000000000..803c826af4f
--- /dev/null
+++ b/jstests/aggregation/variables/remove_system_variable.js
@@ -0,0 +1,87 @@
+/**
+ * Tests for the $$REMOVE system variable.
+ */
+(function() {
+ "use strict";
+
+ let coll = db[jsTest.name()];
+ coll.drop();
+
+ assert.writeOK(coll.insert({_id: 1, a: 2, b: 3}));
+ assert.writeOK(coll.insert({_id: 2, a: 3, b: 4}));
+ assert.writeOK(coll.insert({_id: 3, a: {b: 98, c: 99}}));
+
+ let projectStage = {
+ $project: {_id: 0, a: 1, b: {$cond: {if: {$eq: ["$b", 4]}, then: "$$REMOVE", else: "$b"}}}
+ };
+
+ // Test that we can conditionally remove a field in $project.
+ assert.eq([{a: 2, b: 3}], coll.aggregate([{$match: {_id: 1}}, projectStage]).toArray());
+ assert.eq([{a: 3}], coll.aggregate([{$match: {_id: 2}}, projectStage]).toArray());
+
+ // Test removal of a nested field, using $project.
+ assert.eq([{a: {b: 98}}],
+ coll.aggregate([{$match: {_id: 3}}, {$project: {_id: 0, "a.b": 1}}]).toArray());
+ assert.eq(
+ [{a: {}}],
+ coll.aggregate([{$match: {_id: 3}}, {$project: {_id: 0, "a.b": "$$REMOVE"}}]).toArray());
+ assert.eq(
+ [{a: {}}],
+ coll.aggregate([{$match: {_id: 3}}, {$project: {_id: 0, a: {b: "$$REMOVE"}}}]).toArray());
+
+ // Test removal of a nested field, using $addFields.
+ assert.eq([{_id: 3, a: {c: 99}}],
+ coll.aggregate([{$match: {_id: 3}}, {$addFields: {"a.b": "$$REMOVE"}}]).toArray());
+
+ // Test that any field path following "$$REMOVE" also evaluates to missing.
+ assert.eq([{_id: 3}],
+ coll.aggregate([{$match: {_id: 3}}, {$addFields: {"a": "$$REMOVE.a.c"}}]).toArray());
+
+ // Test that $$REMOVE can be used together with user-defined variables in a $let.
+ assert.eq([{a: {b: 3, d: 4}}],
+ coll.aggregate([
+ {$match: {_id: 3}},
+ {
+ $project: {
+ _id: 0,
+ a: {
+ $let: {
+ vars: {bar: 3, foo: 4},
+ in : {b: "$$bar", c: "$$REMOVE", d: "$$foo"}
+ }
+ }
+ }
+ }
+ ])
+ .toArray());
+
+ // Test that $$REMOVE cannot be assigned in a $let.
+ assert.commandFailedWithCode(db.runCommand({
+ aggregate: coll.getName(),
+ cursor: {},
+ pipeline: [
+ {$match: {_id: 3}},
+ {$project: {_id: 0, a: {$let: {vars: {"REMOVE": 3}, in : {b: "$$REMOVE", c: 2}}}}}
+ ]
+ }),
+ 16867);
+
+ // Test that $$REMOVE, $$CURRENT, $$ROOT, and user-defined variables can all be used together.
+ assert.eq(
+ [{a: {b: 3, d: {_id: 1, a: 2, b: 3}, e: {_id: 1, a: 2, b: 3}}}],
+ coll.aggregate([
+ {$match: {_id: 1}},
+ {
+ $project: {
+ _id: 0,
+ a: {
+ $let: {
+ vars: {myVar: 3},
+ in : {b: "$$myVar", c: "$$REMOVE", d: "$$ROOT", e: "$$CURRENT"}
+ }
+ }
+ }
+ }
+ ])
+ .toArray());
+}());