1
t = db.find_and_modify;
5
for(var i=1; i<=10; i++) {
6
t.insert({priority:i, inprogress:false, value:0});
10
out = t.findAndModify({update: {$set: {inprogress: true}, $inc: {value:1}}});
11
assert.eq(out.value, 0);
12
assert.eq(out.inprogress, false);
13
t.update({_id: out._id}, {$set: {inprogress: false}});
16
out = t.findAndModify({update: {$set: {inprogress: true}, $inc: {value:1}}, 'new': true});
17
assert.eq(out.value, 2);
18
assert.eq(out.inprogress, true);
19
t.update({_id: out._id}, {$set: {inprogress: false}});
21
// update highest priority
22
out = t.findAndModify({query: {inprogress:false}, sort:{priority:-1}, update: {$set: {inprogress: true}}});
23
assert.eq(out.priority, 10);
24
// update next highest priority
25
out = t.findAndModify({query: {inprogress:false}, sort:{priority:-1}, update: {$set: {inprogress: true}}});
26
assert.eq(out.priority, 9);
28
// remove lowest priority
29
out = t.findAndModify({sort:{priority:1}, remove:true});
30
assert.eq(out.priority, 1);
32
// remove next lowest priority
33
out = t.findAndModify({sort:{priority:1}, remove:1});
34
assert.eq(out.priority, 2);
36
// return empty obj if no matches (drivers may handle this differently)
37
out = t.findAndModify({query:{no_such_field:1}, remove:1});