Files
mongo/jstests/core/shell/collection_save.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

32 lines
1.1 KiB
JavaScript

/**
* Tests 'db.collection.save()' mongo shell method.
*
*/
const testDB = db.getSiblingDB(jsTestName());
assert.commandWorked(testDB.dropDatabase());
const coll = testDB["collsave"];
let res;
// Saving a document without '_id' should trigger insert operation.
res = assert.commandWorked(coll.save({fruit: "cherry"}));
assert.eq(res.nInserted, 1);
// Saving a document with '_id' should trigger upsert operation.
res = assert.commandWorked(coll.save({_id: 0, fruit: "pear"}));
assert.eq(res.nUpserted, 1);
assert.eq(res.nModified, 0);
res = assert.commandWorked(coll.save({_id: 0, fruit: "pear updated"}));
assert.eq(res.nUpserted, 0);
assert.eq(res.nModified, 1);
// Verify forbidden cases,
assert.throws(() => coll.save(null), [], "saving null must throw an error");
assert.throws(() => coll.save(42), [], "saving a number must throw an error");
assert.throws(
() => coll.save("The answer to life, the universe and everything"),
[],
"saving a string must throw an error",
);
assert.throws(() => coll.save([{"fruit": "mango"}, {"fruit": "orange"}]), [], "saving an array must throw an error");