summaryrefslogtreecommitdiff
path: root/jstests/core/find_and_modify.js
blob: 394b618f10919aba98fb1f3d2173b589f0a60fdb (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
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
t = db.find_and_modify;
t.drop();

// fill db
for (var i = 1; i <= 10; i++) {
    t.insert({priority: i, inprogress: false, value: 0});
}

// returns old
out = t.findAndModify({update: {$set: {inprogress: true}, $inc: {value: 1}}});
assert.eq(out.value, 0);
assert.eq(out.inprogress, false);
t.update({_id: out._id}, {$set: {inprogress: false}});

// returns new
out = t.findAndModify({update: {$set: {inprogress: true}, $inc: {value: 1}}, 'new': true});
assert.eq(out.value, 2);
assert.eq(out.inprogress, true);
t.update({_id: out._id}, {$set: {inprogress: false}});

// update highest priority
out = t.findAndModify(
    {query: {inprogress: false}, sort: {priority: -1}, update: {$set: {inprogress: true}}});
assert.eq(out.priority, 10);
// update next highest priority
out = t.findAndModify(
    {query: {inprogress: false}, sort: {priority: -1}, update: {$set: {inprogress: true}}});
assert.eq(out.priority, 9);

// remove lowest priority
out = t.findAndModify({sort: {priority: 1}, remove: true});
assert.eq(out.priority, 1);

// remove next lowest priority
out = t.findAndModify({sort: {priority: 1}, remove: 1});
assert.eq(out.priority, 2);

// return null (was {} before 1.5.4) if no matches (drivers may handle this differently)
out = t.findAndModify({query: {no_such_field: 1}, remove: 1});
assert.eq(out, null);

// make sure we fail with conflicting params to findAndModify SERVER-16601
t.insert({x: 1});
assert.throws(function() {
    t.findAndModify({query: {x: 1}, update: {y: 2}, remove: true});
});
assert.throws(function() {
    t.findAndModify({query: {x: 1}, update: {y: 2}, remove: true, sort: {x: 1}});
});
assert.throws(function() {
    t.findAndModify({query: {x: 1}, update: {y: 2}, remove: true, upsert: true});
});
assert.throws(function() {
    t.findAndModify({query: {x: 1}, update: {y: 2}, new: true, remove: true});
});
assert.throws(function() {
    t.findAndModify({query: {x: 1}, upsert: true, remove: true});
});

//
// SERVER-17387: Find and modify should throw in the case of invalid projection.
//

t.drop();

// Insert case.
var cmdRes = db.runCommand({
    findAndModify: t.getName(),
    query: {_id: "miss"},
    update: {$inc: {y: 1}},
    fields: {foo: {$pop: ["bar"]}},
    upsert: true,
    new: true
});
assert.commandFailed(cmdRes);

t.insert({_id: "found"});

// Update with upsert + new.
cmdRes = db.runCommand({
    findAndModify: t.getName(),
    query: {_id: "found"},
    update: {$inc: {y: 1}},
    fields: {foo: {$pop: ["bar"]}},
    upsert: true,
    new: true
});
assert.commandFailed(cmdRes);

// Update with just new: true.
cmdRes = db.runCommand({
    findAndModify: t.getName(),
    query: {_id: "found"},
    update: {$inc: {y: 1}},
    fields: {foo: {$pop: ["bar"]}},
    new: true
});
assert.commandFailed(cmdRes);

// Update with just upsert: true.
cmdRes = db.runCommand({
    findAndModify: t.getName(),
    query: {_id: "found"},
    update: {$inc: {y: 1}},
    fields: {foo: {$pop: ["bar"]}},
    upsert: true
});
assert.commandFailed(cmdRes);

// Update with neither upsert nor new flags.
cmdRes = db.runCommand({
    findAndModify: t.getName(),
    query: {_id: "found"},
    update: {$inc: {y: 1}},
    fields: {foo: {$pop: ["bar"]}},
});
assert.commandFailed(cmdRes);

//
// SERVER-17372
//

t.drop();
cmdRes = db.runCommand(
    {findAndModify: t.getName(), query: {_id: "miss"}, update: {$inc: {y: 1}}, upsert: true});
assert.commandWorked(cmdRes);
assert("value" in cmdRes);
assert.eq(null, cmdRes.value);

cmdRes = db.runCommand({
    findAndModify: t.getName(),
    query: {_id: "missagain"},
    update: {$inc: {y: 1}},
    upsert: true,
    new: true
});
assert.commandWorked(cmdRes);
assert("value" in cmdRes);
assert.eq("missagain", cmdRes.value._id);

// Two upserts should have happened.
assert.eq(2, t.count());