summaryrefslogtreecommitdiff
path: root/jstests/find_and_modify4.js
blob: 5f343185450e7b41090b9a55983363882a8c83b9 (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
t = db.find_and_modify4;
t.drop();

// this is the best way to build auto-increment
function getNextVal(counterName){
    var ret = t.findAndModify({
                query: {_id: counterName},
                update: {$inc: {val: 1}},
                upsert: true,
                'new': true,
                });
    return ret.val;
}

assert.eq(getNextVal("a"), 1);
assert.eq(getNextVal("a"), 2);
assert.eq(getNextVal("a"), 3);
assert.eq(getNextVal("z"), 1);
assert.eq(getNextVal("z"), 2);
assert.eq(getNextVal("a"), 4);

t.drop();

function helper(upsert){
    return t.findAndModify({
                query: {_id: "asdf"},
                update: {$inc: {val: 1}},
                upsert: upsert, 
                'new': false // the default
                });
}

// upsert:false so nothing there before and after
assert.eq(helper(false), null);
assert.eq(t.count(), 0);

// upsert:false so nothing there before; something there after
assert.eq(helper(true), null);
assert.eq(t.count(), 1);
assert.eq(helper(true),  {_id: 'asdf', val: 1});
assert.eq(helper(false), {_id: 'asdf', val: 2}); // upsert only matters when obj doesn't exist
assert.eq(helper(true),  {_id: 'asdf', val: 3});


// _id created if not specified
var out = t.findAndModify({
                query: {a:1},
                update: {$set: {b: 2}},
                upsert: true, 
                'new': true
                });
assert.neq(out._id, undefined);
assert.eq(out.a, 1);
assert.eq(out.b, 2);