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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
//
// Upsert behavior tests for field extraction
//
var coll = db.upsert_field_gen;
coll.drop();
var upsertedResult = function(query, expr) {
coll.drop();
result = coll.update(query, expr, {upsert: true});
return result;
};
var upsertedField = function(query, expr, fieldName) {
var res = assert.writeOK(upsertedResult(query, expr));
var doc = coll.findOne();
assert.neq(doc, null, "findOne query returned no results! UpdateRes: " + tojson(res));
return doc[fieldName];
};
var upsertedId = function(query, expr) {
return upsertedField(query, expr, "_id");
};
var upsertedXVal = function(query, expr) {
return upsertedField(query, expr, "x");
};
//
// _id field has special rules
//
// _id autogenerated
assert.neq(null, upsertedId({}, {}));
// _id autogenerated with add'l fields
assert.neq(null, upsertedId({}, {a: 1}));
assert.eq(1, upsertedField({}, {a: 1}, "a"));
assert.neq(null, upsertedId({}, {$set: {a: 1}}, "a"));
assert.eq(1, upsertedField({}, {$set: {a: 1}}, "a"));
assert.neq(null, upsertedId({}, {$setOnInsert: {a: 1}}, "a"));
assert.eq(1, upsertedField({}, {$setOnInsert: {a: 1}}, "a"));
// _id not autogenerated
assert.eq(1, upsertedId({}, {_id: 1}));
assert.eq(1, upsertedId({}, {$set: {_id: 1}}));
assert.eq(1, upsertedId({}, {$setOnInsert: {_id: 1}}));
// _id type error
assert.writeError(upsertedResult({}, {_id: [1, 2]}));
assert.writeError(upsertedResult({}, {_id: undefined}));
assert.writeError(upsertedResult({}, {$set: {_id: [1, 2]}}));
// Fails in v2.6, no validation
assert.writeError(upsertedResult({}, {$setOnInsert: {_id: undefined}}));
// Check things that are pretty much the same for replacement and $op style upserts
for (var i = 0; i < 3; i++) {
// replacement style
var expr = {};
// $op style
if (i == 1)
expr = {$set: {a: 1}};
if (i == 2)
expr = {$setOnInsert: {a: 1}};
var isReplStyle = i == 0;
// _id extracted
assert.eq(1, upsertedId({_id: 1}, expr));
// All below fail in v2.6, no $ops for _id and $and/$or not explored
assert.eq(1, upsertedId({_id: {$eq: 1}}, expr));
assert.eq(1, upsertedId({_id: {$all: [1]}}, expr));
assert.eq(1, upsertedId({_id: {$in: [1]}}, expr));
assert.eq(1, upsertedId({$and: [{_id: 1}]}, expr));
assert.eq(1, upsertedId({$and: [{_id: {$eq: 1}}]}, expr));
assert.eq(1, upsertedId({$or: [{_id: 1}]}, expr));
assert.eq(1, upsertedId({$or: [{_id: {$eq: 1}}]}, expr));
// _id not extracted, autogenerated
assert.neq(1, upsertedId({_id: {$gt: 1}}, expr));
assert.neq(1, upsertedId({_id: {$ne: 1}}, expr));
assert.neq(1, upsertedId({_id: {$in: [1, 2]}}, expr));
assert.neq(1, upsertedId({_id: {$elemMatch: {$eq: 1}}}, expr));
assert.neq(1, upsertedId({_id: {$exists: true}}, expr));
assert.neq(1, upsertedId({_id: {$not: {$eq: 1}}}, expr));
assert.neq(1, upsertedId({$or: [{_id: 1}, {_id: 1}]}, expr));
assert.neq(1, upsertedId({$or: [{_id: {$eq: 1}}, {_id: 2}]}, expr));
assert.neq(1, upsertedId({$nor: [{_id: 1}]}, expr));
assert.neq(1, upsertedId({$nor: [{_id: {$eq: 1}}]}, expr));
assert.neq(1, upsertedId({$nor: [{_id: {$eq: 1}}, {_id: 1}]}, expr));
// _id extraction errors
assert.writeError(upsertedResult({_id: [1, 2]}, expr));
assert.writeError(upsertedResult({_id: undefined}, expr));
assert.writeError(upsertedResult({_id: {$eq: [1, 2]}}, expr));
assert.writeError(upsertedResult({_id: {$eq: undefined}}, expr));
assert.writeError(upsertedResult({_id: {$all: [1, 2]}}, expr));
// All below fail in v2.6, non-_id fields completely ignored
assert.writeError(upsertedResult({$and: [{_id: 1}, {_id: 1}]}, expr));
assert.writeError(upsertedResult({$and: [{_id: {$eq: 1}}, {_id: 2}]}, expr));
assert.writeError(upsertedResult({_id: 1, "_id.x": 1}, expr));
assert.writeError(upsertedResult({_id: {x: 1}, "_id.x": 1}, expr));
// Special case - nested _id fields only used on $op-style updates
if (isReplStyle) {
// Fails in v2.6
assert.writeError(upsertedResult({"_id.x": 1, "_id.y": 2}, expr));
} else {
// Fails in v2.6
assert.docEq({x: 1, y: 2}, upsertedId({"_id.x": 1, "_id.y": 2}, expr));
}
}
// regex _id in expression is an error, no regex ids allowed
assert.writeError(upsertedResult({}, {_id: /abc/}));
// Fails in v2.6, no validation
assert.writeError(upsertedResult({}, {$set: {_id: /abc/}}));
// no regex _id extraction from query
assert.neq(/abc/, upsertedId({_id: /abc/}, {}));
//
// Regular field extraction
//
// Check things that are pretty much the same for replacement and $op style upserts
for (var i = 0; i < 3; i++) {
// replacement style
var expr = {};
// $op style
if (i == 1)
expr = {$set: {a: 1}};
if (i == 2)
expr = {$setOnInsert: {a: 1}};
var isReplStyle = i == 0;
// field extracted when replacement style
var value = isReplStyle ? undefined : 1;
assert.eq(value, upsertedXVal({x: 1}, expr));
assert.eq(value, upsertedXVal({x: {$eq: 1}}, expr));
assert.eq(value, upsertedXVal({x: {$in: [1]}}, expr));
assert.eq(value, upsertedXVal({x: {$all: [1]}}, expr));
assert.eq(value, upsertedXVal({$and: [{x: 1}]}, expr));
assert.eq(value, upsertedXVal({$and: [{x: {$eq: 1}}]}, expr));
assert.eq(value, upsertedXVal({$or: [{x: 1}]}, expr));
assert.eq(value, upsertedXVal({$or: [{x: {$eq: 1}}]}, expr));
// Special types extracted
assert.eq(isReplStyle ? undefined : [1, 2], upsertedXVal({x: [1, 2]}, expr));
// field not extracted
assert.eq(undefined, upsertedXVal({x: {$gt: 1}}, expr));
assert.eq(undefined, upsertedXVal({x: {$ne: 1}}, expr));
assert.eq(undefined, upsertedXVal({x: {$in: [1, 2]}}, expr));
assert.eq(undefined, upsertedXVal({x: {$elemMatch: {$eq: 1}}}, expr));
assert.eq(undefined, upsertedXVal({x: {$exists: true}}, expr));
assert.eq(undefined, upsertedXVal({x: {$not: {$eq: 1}}}, expr));
assert.eq(undefined, upsertedXVal({$or: [{x: 1}, {x: 1}]}, expr));
assert.eq(undefined, upsertedXVal({$or: [{x: {$eq: 1}}, {x: 2}]}, expr));
assert.eq(undefined, upsertedXVal({$nor: [{x: 1}]}, expr));
assert.eq(undefined, upsertedXVal({$nor: [{x: {$eq: 1}}]}, expr));
assert.eq(undefined, upsertedXVal({$nor: [{x: {$eq: 1}}, {x: 1}]}, expr));
// field extraction errors
assert.writeError(upsertedResult({x: undefined}, expr));
if (!isReplStyle) {
assert.writeError(upsertedResult({x: {'x.x': 1}}, expr));
assert.writeError(upsertedResult({x: {$all: [1, 2]}}, expr));
assert.writeError(upsertedResult({$and: [{x: 1}, {x: 1}]}, expr));
assert.writeError(upsertedResult({$and: [{x: {$eq: 1}}, {x: 2}]}, expr));
} else {
assert.eq(undefined, upsertedXVal({x: {'x.x': 1}}, expr));
assert.eq(undefined, upsertedXVal({x: {$all: [1, 2]}}, expr));
assert.eq(undefined, upsertedXVal({$and: [{x: 1}, {x: 1}]}, expr));
assert.eq(undefined, upsertedXVal({$and: [{x: {$eq: 1}}, {x: 2}]}, expr));
}
// nested field extraction
var docValue = isReplStyle ? undefined : {x: 1};
assert.docEq(docValue, upsertedXVal({"x.x": 1}, expr));
assert.docEq(docValue, upsertedXVal({"x.x": {$eq: 1}}, expr));
assert.docEq(docValue, upsertedXVal({"x.x": {$all: [1]}}, expr));
assert.docEq(docValue, upsertedXVal({$and: [{"x.x": 1}]}, expr));
assert.docEq(docValue, upsertedXVal({$and: [{"x.x": {$eq: 1}}]}, expr));
assert.docEq(docValue, upsertedXVal({$or: [{"x.x": 1}]}, expr));
assert.docEq(docValue, upsertedXVal({$or: [{"x.x": {$eq: 1}}]}, expr));
// nested field conflicts
if (!isReplStyle) {
assert.writeError(upsertedResult({x: 1, "x.x": 1}, expr));
assert.writeError(upsertedResult({x: {}, "x.x": 1}, expr));
assert.writeError(upsertedResult({x: {x: 1}, "x.x": 1}, expr));
assert.writeError(upsertedResult({x: {x: 1}, "x.y": 1}, expr));
assert.writeError(upsertedResult({x: [1, {x: 1}], "x.x": 1}, expr));
} else {
assert.eq(undefined, upsertedXVal({x: 1, "x.x": 1}, expr));
assert.eq(undefined, upsertedXVal({x: {}, "x.x": 1}, expr));
assert.eq(undefined, upsertedXVal({x: {x: 1}, "x.x": 1}, expr));
assert.eq(undefined, upsertedXVal({x: {x: 1}, "x.y": 1}, expr));
assert.eq(undefined, upsertedXVal({x: [1, {x: 1}], "x.x": 1}, expr));
}
}
// regex field in expression is a value
assert.eq(/abc/, upsertedXVal({}, {x: /abc/}));
assert.eq(/abc/, upsertedXVal({}, {$set: {x: /abc/}}));
// no regex field extraction from query unless $eq'd
assert.eq(/abc/, upsertedXVal({x: {$eq: /abc/}}, {$set: {a: 1}}));
assert.eq(undefined, upsertedXVal({x: /abc/}, {$set: {a: 1}}));
// replacement-style updates ignore conflicts *except* on _id field
assert.eq(1, upsertedId({_id: 1, x: [1, {x: 1}], "x.x": 1}, {}));
// DBRef special cases
// make sure query doesn't error when creating doc for insert, since it's missing the rest of the
// dbref fields. SERVER-14024
// Fails in 2.6.1->3
assert.docEq(tojson(DBRef("a", 1)), upsertedXVal({"x.$id": 1}, {$set: {x: DBRef("a", 1)}}));
|