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

(function() {
"use strict";

const st = new ShardingTest({nodes: 2});

const dbName = 'testDb';
const collName = 'testColl';
const coll = st.s.getDB(dbName).getCollection(collName);

st.adminCommand({enablesharding: dbName});
const oplog = st.getPrimaryShard(dbName).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();
})();

st.stop();
}());