-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathms0.js
63 lines (51 loc) · 1.75 KB
/
ms0.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Author: <>
* This is for referential purposes and is not meant to be production ready code.
* There are no warranties associated with these functions
* in order to run this, execute the script and include command line arguments to perform various functions
* for example, run "node ms0.js insert" to create a collection and insert a sample document
*/
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://main_user:[email protected]?retryWrites=true&w=majority";
const client = new MongoClient(uri, { serverApi: ServerApiVersion.v1 });
const args = process.argv;
console.log(args);
let db = client.db("MongoSprint");
let collection = db.collection('NodeSandbox');
let doc1 = {
"a": 1,
"example": true,
"time": new Date()
};
let doc2 = {
"a": 3,
"example": false,
"time": new Date(),
"newField": "new"
}
if (args.includes('insert')) {
collection.insertOne(doc1);
}
if (args.includes('updateMatched')) {
collection.updateMany({ "a": 1 }, { "$unset": { "updated": true } });
}
if (args.includes('updateUnmatched')) {
let updateResponse = collection.updateMany({ "a": 2 }, { "$set": { "blah": true } });
console.log(JSON.stringify(updateResponse, null, 2));
}
if (args.includes('replaceMatched')) {
collection.replaceOne({ "a": 1 }, doc2);
}
if (args.includes('upsert')) {
collection.updateMany({ "a": 2 }, { "$set": { "updated": true } }, { upsert: true });
}
if (args.includes('reset')) {
collection.deleteMany({});
}
if (args.includes('currentop')) {
db = client.db('admin');
let updateResponse = db.aggregate([{ "$currentOp": {} }]);
updateResponse.forEach(document => {
console.log(JSON.stringify(document, null, 2));
});
}