summaryrefslogtreecommitdiff
path: root/jstests/core/update_array_offset_positional.js
blob: 210e4d65bb79bbd308897d45ac4cdd6a8a787a44 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
 * Tests that array offset matches are not used to provide values for the positional operator.
 */
(function() {
"use strict";

let coll = db.jstest_update_array_offset_positional;
coll.drop();

//
// If there is no implicit array traversal, the positional operator cannot be used.
//

coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [0]}));
assert.writeError(coll.update({_id: 0, "a.0": 0}, {$set: {"a.$": 1}}));

coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [{b: 0}]}));
assert.writeError(coll.update({_id: 0, "a.0.b": 0}, {$set: {"a.$.b": 1}}));

coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [[0]]}));
assert.writeError(coll.update({_id: 0, "a.0.0": 0}, {$set: {"a.$.0": 1}}));

coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [{b: [0]}]}));
assert.writeError(coll.update({_id: 0, "a.0.b.0": 0}, {$set: {"a.$.b.0": 1}}));

//
// Array offset matches are not used to provide values for the positional operator on the same
// path.
//

coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [{b: [0, 1]}]}));
assert.commandWorked(coll.update({_id: 0, "a.0.b": 1}, {$set: {"a.0.b.$": 2}}));
assert.eq(coll.findOne({_id: 0}), {_id: 0, a: [{b: [0, 2]}]});

coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [{b: [0, 1]}]}));
assert.commandWorked(coll.update({_id: 0, "a.b.1": 1}, {$set: {"a.$.b.1": 2}}));
assert.eq(coll.findOne({_id: 0}), {_id: 0, a: [{b: [0, 2]}]});

//
// Array offset matches are not used to provide values for the positional operator on a
// different path.
//

coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [0, 1], b: [0]}));
assert.commandWorked(coll.update({_id: 0, a: 1, "b.0": 0}, {$set: {"a.$": 2}}));
assert.eq(coll.findOne({_id: 0}), {_id: 0, a: [0, 2], b: [0]});

coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [0, 1], b: [{c: 0}]}));
assert.commandWorked(coll.update({_id: 0, a: 1, "b.0.c": 0}, {$set: {"a.$": 2}}));
assert.eq(coll.findOne({_id: 0}), {_id: 0, a: [0, 2], b: [{c: 0}]});

coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [0, 1], b: [[0]]}));
assert.commandWorked(coll.update({_id: 0, a: 1, "b.0.0": 0}, {$set: {"a.$": 2}}));
assert.eq(coll.findOne({_id: 0}), {_id: 0, a: [0, 2], b: [[0]]});

coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [0, 1], b: [{c: [0]}]}));
assert.commandWorked(coll.update({_id: 0, a: 1, "b.0.c.0": 0}, {$set: {"a.$": 2}}));
assert.eq(coll.findOne({_id: 0}), {_id: 0, a: [0, 2], b: [{c: [0]}]});
}());