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
|
// SERVER-4516 and SERVER-6913: test that update and findAndModify tolerate
// an _id in the update document, as long as the _id will not be modified
var t = db.jstests_server4516;
var startingDoc = {_id: 1, a: 1};
function prepare() {
t.drop();
t.save(startingDoc);
}
function update_succeeds(updateDoc, qid, resultDoc) {
prepare();
t.update({_id: qid}, updateDoc, true);
assert.eq(t.findOne({_id: qid}), resultDoc);
prepare();
t.findAndModify({query: {_id: qid}, update: updateDoc, upsert: true});
assert.eq(t.findOne({_id: qid}), resultDoc);
}
update_succeeds({_id: 1, a: 2}, 1, {_id: 1, a: 2});
update_succeeds({$set: {_id: 1}}, 1, {_id: 1, a: 1});
update_succeeds({_id: 1, b: "a"}, 1, {_id: 1, b: "a"});
update_succeeds({_id: 2, a: 3}, 2, {_id: 2, a: 3});
function update_fails(updateDoc, qid) {
prepare();
var res = t.update({_id: qid}, updateDoc, true);
assert.writeError(res);
assert.eq(t.count(), 1);
assert.eq(t.findOne(), startingDoc);
prepare();
assert.throws(function () {
t.findAndModify({query: {_id: qid}, update: updateDoc, upsert: true});
});
assert.eq(t.count(), 1);
assert.eq(t.findOne(), startingDoc);
}
update_fails({$set: {_id: 2}}, 1);
update_fails({_id: 2, a: 3}, 1);
update_fails({_id: 2, a: 3}, 3);
|