diff options
author | James Wahlin <james@mongodb.com> | 2019-05-15 10:06:19 -0400 |
---|---|---|
committer | James Wahlin <james@mongodb.com> | 2019-05-17 11:16:24 -0400 |
commit | 17fe1b7109a93402ec77afee92c1b4a5118a45d4 (patch) | |
tree | 5888d18504be9a0993c42fd641202b4421f096a0 /jstests/aggregation/sources | |
parent | c9bdb7e64265bf36cb3ca81edd8cb36ed0589a04 (diff) | |
download | mongo-17fe1b7109a93402ec77afee92c1b4a5118a45d4.tar.gz |
SERVER-40240 Add new $unset aggregation stage
Diffstat (limited to 'jstests/aggregation/sources')
-rw-r--r-- | jstests/aggregation/sources/unset/unset.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/jstests/aggregation/sources/unset/unset.js b/jstests/aggregation/sources/unset/unset.js new file mode 100644 index 00000000000..70adb54db8d --- /dev/null +++ b/jstests/aggregation/sources/unset/unset.js @@ -0,0 +1,35 @@ +// Basic testing for the $unset aggregation stage. +(function() { + "use strict"; + + load("jstests/aggregation/extras/utils.js"); // For assertArrayEq. + + const coll = db.agg_stage_unset; + coll.drop(); + + assert.commandWorked(coll.insert( + [{_id: 0, a: 10}, {_id: 1, a: {b: 20, c: 30, 0: 40}}, {_id: 2, a: [{b: 50, c: 60}]}])); + + // unset single field. + let result = coll.aggregate([{$unset: ["a"]}]).toArray(); + assertArrayEq({actual: result, expected: [{_id: 0}, {_id: 1}, {_id: 2}]}); + + // unset multiple fields. + result = coll.aggregate([{$unset: ["_id", "a"]}]).toArray(); + assertArrayEq({actual: result, expected: [{}, {}, {}]}); + + // unset with dotted field path. + result = coll.aggregate([{$unset: ["a.b"]}]).toArray(); + assertArrayEq({ + actual: result, + expected: [{_id: 0, a: 10}, {_id: 1, a: {0: 40, c: 30}}, {_id: 2, a: [{c: 60}]}] + }); + + // Numeric field paths in aggregation represent field name only and not array offset. + result = coll.aggregate([{$unset: ["a.0"]}]).toArray(); + assertArrayEq({ + actual: result, + expected: [{_id: 0, a: 10}, {_id: 1, a: {b: 20, c: 30}}, {_id: 2, a: [{b: 50, c: 60}]}] + }); + +})(); |