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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// Cannot implicitly shard accessed collections because of following errmsg: A single
// update/delete on a sharded collection must contain an exact match on _id or contain the shard
// key.
//
// @tags: [assumes_unsharded_collection, requires_fastcount]
t = db.update_addToSet1;
t.drop();
o = {
_id: 1,
a: [2, 1]
};
t.insert(o);
assert.eq(o, t.findOne(), "A1");
t.update({}, {$addToSet: {a: 3}});
o.a.push(3);
assert.eq(o, t.findOne(), "A2");
t.update({}, {$addToSet: {a: 3}});
assert.eq(o, t.findOne(), "A3");
// SERVER-628
t.update({}, {$addToSet: {a: {$each: [3, 5, 6]}}});
o.a.push(5);
o.a.push(6);
assert.eq(o, t.findOne(), "B1");
t.drop();
o = {
_id: 1,
a: [3, 5, 6]
};
t.insert(o);
t.update({}, {$addToSet: {a: {$each: [3, 5, 6]}}});
assert.eq(o, t.findOne(), "B2");
t.drop();
t.update({_id: 1}, {$addToSet: {a: {$each: [3, 5, 6]}}}, true);
assert.eq(o, t.findOne(), "B3");
t.update({_id: 1}, {$addToSet: {a: {$each: [3, 5, 6]}}}, true);
assert.eq(o, t.findOne(), "B4");
// SERVER-630
t.drop();
t.update({_id: 2}, {$addToSet: {a: 3}}, true);
assert.eq(1, t.count(), "C1");
assert.eq({_id: 2, a: [3]}, t.findOne(), "C2");
// SERVER-3245
o = {
_id: 1,
a: [1, 2]
};
t.drop();
t.update({_id: 1}, {$addToSet: {a: {$each: [1, 2]}}}, true);
assert.eq(o, t.findOne(), "D1");
t.drop();
t.update({_id: 1}, {$addToSet: {a: {$each: [1, 2, 1, 2]}}}, true);
assert.eq(o, t.findOne(), "D2");
t.drop();
t.insert({_id: 1});
t.update({_id: 1}, {$addToSet: {a: {$each: [1, 2, 2, 1]}}});
assert.eq(o, t.findOne(), "D3");
t.update({_id: 1}, {$addToSet: {a: {$each: [3, 2, 2, 3, 3]}}});
o.a.push(3);
assert.eq(o, t.findOne(), "D4");
// Test that dotted and '$' prefixed field names fail.
t.drop();
o = {
_id: 1,
a: [1, 2]
};
assert.commandWorked(t.insert(o));
assert.commandWorked(t.update({}, {$addToSet: {a: {'x.$.y': 'bad'}}}));
assert.commandWorked(t.update({}, {$addToSet: {a: {b: {'x.$.y': 'bad'}}}}));
assert.writeError(t.update({}, {$addToSet: {a: {"$bad": "bad"}}}));
assert.writeError(t.update({}, {$addToSet: {a: {b: {"$bad": "bad"}}}}));
assert.commandWorked(t.update({}, {$addToSet: {a: {_id: {"x.y": 2}}}}));
assert.commandWorked(t.update({}, {$addToSet: {a: {$each: [{'x.$.y': 'bad'}]}}}));
assert.commandWorked(t.update({}, {$addToSet: {a: {$each: [{b: {'x.$.y': 'bad'}}]}}}));
assert.writeError(t.update({}, {$addToSet: {a: {$each: [{'$bad': 'bad'}]}}}));
assert.writeError(t.update({}, {$addToSet: {a: {$each: [{b: {'$bad': 'bad'}}]}}}));
// Test that nested _id fields are allowed.
t.drop();
o = {
_id: 1,
a: [1, 2]
};
assert.commandWorked(t.insert(o));
assert.commandWorked(t.update({}, {$addToSet: {a: {_id: ["foo", "bar", "baz"]}}}));
assert.commandWorked(t.update({}, {$addToSet: {a: {_id: /acme.*corp/}}}));
// Test that DBRefs are allowed.
t.drop();
o = {
_id: 1,
a: [1, 2]
};
assert.commandWorked(t.insert(o));
foo = {
"foo": "bar"
};
assert.commandWorked(t.insert(foo));
let fooDoc = t.findOne(foo);
assert.eq(fooDoc.foo, foo.foo);
let fooDocRef = {reference: new DBRef(t.getName(), fooDoc._id, t.getDB().getName())};
assert.commandWorked(t.update({_id: o._id}, {$addToSet: {a: fooDocRef}}));
assert.eq(t.findOne({_id: o._id}).a[2], fooDocRef);
assert.commandWorked(t.update({_id: o._id}, {$addToSet: {a: {b: fooDocRef}}}));
assert.eq(t.findOne({_id: o._id}).a[3].b, fooDocRef);
|