summaryrefslogtreecommitdiff
path: root/jstests/core/apply_ops_missing_field.js
blob: d80eb30e1acaddf93918ac576b937a97a8a8bc36 (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
/**
 * Test that applyOps ignores updates intended for array elements or subdocument fields, if the
 * target document lacks the expected array/subdocument. This can arise when re-applying past
 * operations that happened before the array/subdoc field was removed. The applyOps command must be
 * able to continue past these errors for the sake of idempotency.
 *
 * Additionally, if an update to one field fails, updates to other fields in the same applyOps
 * operation should succeed.
 *
 * The original motivation for this test is MongoMirror.
 *
 * @tags: [
 *     requires_non_retryable_commands,
 *
 *     # applyOps uses the oplog which requires replication support.
 *     requires_replication,
 * ]
 */

(function() {
"use strict";

const coll = db.getCollection(jsTestName());
coll.drop();

// Run applyOps as MongoMirror does, with a dummy command to make it non-atomic. MongoMirror does
// not use allowAtomic: false.
function applyOps(ops) {
    ops.push({op: "c", ns: "admin.$cmd", o: {applyOps: [{op: "n", ns: "", o: {"msg": "noop"}}]}});
    let command = {applyOps: ops, writeConcern: {w: "majority"}};
    assert.commandWorked(db.adminCommand(command));
}

const originalDoc = {
    _id: 1,
    a: null
};

coll.insert(originalDoc);

// Update field "a", which is null, but apply an operation intended for an array.
applyOps([{ns: coll.getFullName(), op: "u", o2: {_id: 1}, o: {$set: {'a.0': 2}}}]);
assert.eq(originalDoc, coll.findOne());

// Same, but also set a new field 'b' to 1.
applyOps([{ns: coll.getFullName(), op: "u", o2: {_id: 1}, o: {$set: {'a.0': 2, b: 1}}}]);
assert.eq({_id: 1, a: null, b: 1}, coll.findOne());

// An operation intended for a subdocument.
applyOps([{ns: coll.getFullName(), op: "u", o2: {_id: 1}, o: {$set: {'a.field': 2}}}]);
assert.eq({_id: 1, a: null, b: 1}, coll.findOne());

// Same, but set 'b' to 2.
applyOps([{ns: coll.getFullName(), op: "u", o2: {_id: 1}, o: {$set: {'a.field': 2, b: 2}}}]);
assert.eq({_id: 1, a: null, b: 2}, coll.findOne());
}());