summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/unset/unset.js
blob: c11f97598f528cf2027ffa0ce2bb01d985bffbea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 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 should work with string directive.
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}]}]
});
})();