forked from mongodb/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_save.js
31 lines (27 loc) · 1.13 KB
/
collection_save.js
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
/**
* 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");