Files
mongo/jstests/sharding/query/update/update_with_dollar_fields.js
Zac 591928c619 SERVER-108478 JS formatted by prettier and remove clang-format (#39656)
GitOrigin-RevId: 6c8f6aded47f260aa4f7c231b17dae3302cb1e04
2025-08-21 17:27:09 +00:00

53 lines
1.5 KiB
JavaScript

/**
* Tests that replacement style update with $v field in the document is correctly applied.
* @tags: [
* requires_fcv_50,
* ]
*/
import {ShardingTest} from "jstests/libs/shardingtest.js";
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({ns: `${dbName}.${collName}`, op: "u"})
.sort({$natural: -1})
.limit(1)
.next();
assert(lastUpdate.o._id, lastUpdate);
}
[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();