summaryrefslogtreecommitdiff
path: root/jstests/replsets/update_with_dollar_fields.js
blob: 9ecb8a4399c30a9777a857bfe45d0fe30a2f66dd (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
/**
 * Tests that replacement style update with $v field in the document is correctly applied.
 * @tags: [
 *  requires_fcv_60,
 * ]
 */

(function() {
"use strict";

const rst = new ReplSetTest({nodes: 2});
rst.startSet();
rst.initiate();

const dbName = 'testDb';
const collName = 'testColl';
const primary = rst.getPrimary();
const coll = primary.getDB(dbName).getCollection(collName);
const oplog = primary.getDB('local').getCollection('oplog.rs');

function assertLastUpdateOplogEntryIsReplacement() {
    const lastUpdate = oplog.find({op: 'u'}).sort({$natural: -1}).limit(1).next();
    assert(lastUpdate.o._id);
}

[true].forEach($v => {
    const _id = assert.commandWorked(coll.insertOne({$v})).insertedId;
    assert.commandWorked(coll.update({_id}, [{$set: {p: 1, q: 1}}]));
    assertLastUpdateOplogEntryIsReplacement();
});

[true, "hello", 0, 1, 2, 3].forEach($v => {
    const _id = assert.commandWorked(coll.insertOne({})).insertedId;
    assert.commandWorked(coll.update(
        {_id},
        [{$replaceWith: {"$setField": {field: {$literal: "$v"}, input: "$$ROOT", value: $v}}}]));
    assertLastUpdateOplogEntryIsReplacement();
});

(function() {
const _id = assert.commandWorked(coll.insertOne({})).insertedId;
assert.commandWorked(coll.update(
    {_id},
    [{$replaceWith: {"$setField": {field: {$literal: "$set"}, input: "$$ROOT", value: {a: 1}}}}]));
assertLastUpdateOplogEntryIsReplacement();
})();

rst.awaitReplication();
rst.stopSet();
}());