summaryrefslogtreecommitdiff
path: root/jstests/core/update_setOnInsert.js
blob: 9457c69f325d72ba816bd091b873d688ee9fb43e (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
// This tests that $setOnInsert works and allow setting the _id
t = db.update_setOnInsert;
var res;

function dotest(useIndex) {
    t.drop();
    if (useIndex) {
        t.createIndex({a: 1});
    }

    t.update({_id: 5}, {$inc: {x: 2}, $setOnInsert: {a: 3}}, true);
    assert.docEq({_id: 5, a: 3, x: 2}, t.findOne());

    t.update({_id: 5}, {$set: {a: 4}}, true);

    t.update({_id: 5}, {$inc: {x: 2}, $setOnInsert: {a: 3}}, true);
    assert.docEq({_id: 5, a: 4, x: 4}, t.findOne());
}

dotest(false);
dotest(true);

// Cases for SERVER-9958 -- Allow _id $setOnInsert during insert (if upsert:true, and not doc found)
t.drop();

res = t.update({_id: 1}, {$setOnInsert: {"_id.a": new Date()}}, true);
assert.writeError(res, "$setOnInsert _id.a worked");

res = t.update({"_id.a": 4}, {$setOnInsert: {"_id.b": 1}}, true);
assert.writeError(res, "$setOnInsert _id.a/b worked");

res = t.update({"_id.a": 4}, {$setOnInsert: {"_id": {a: 4, b: 1}}}, true);
assert.writeError(res, "$setOnInsert _id.a/a+b worked");