~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to jstests/find_and_modify.js

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
t = db.find_and_modify;
 
2
t.drop();
 
3
 
 
4
// fill db
 
5
for(var i=1; i<=10; i++) {
 
6
    t.insert({priority:i, inprogress:false, value:0});
 
7
}
 
8
 
 
9
// returns old
 
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}});
 
14
 
 
15
// returns new
 
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}});
 
20
 
 
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);
 
27
 
 
28
// remove lowest priority
 
29
out = t.findAndModify({sort:{priority:1}, remove:true});
 
30
assert.eq(out.priority, 1);
 
31
 
 
32
// remove next lowest priority
 
33
out = t.findAndModify({sort:{priority:1}, remove:1});
 
34
assert.eq(out.priority, 2);
 
35
 
 
36
// return empty obj if no matches (drivers may handle this differently)
 
37
out = t.findAndModify({query:{no_such_field:1}, remove:1});
 
38
assert.eq(out, {});