forked from mongodb-developer/nodejs-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
55 lines (45 loc) · 1.26 KB
/
index.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
const assert = require('assert');
const { parseArgs } = require('node:util');
const { MongoClient, ServerApiVersion } = require("mongodb");
const options = {
uri: {
type: 'string',
},
strict: {
type: 'boolean',
short: 's',
},
};
const { values } = parseArgs({ options, tokens: false });
const uri = values.uri;
let client;
if (values.strict) {
client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
}
});
} else {
client = new MongoClient(uri);
}
async function run() {
try {
let res = await client.db('test').command({ ping: 1 });
assert.equal(res.ok, 1, 'ping failed');
res = await client.db('test').command({ dropDatabase: 1 });
assert.equal(res.ok, 1, 'dropDatabase failed');
let docs = [];
for (let i = 1; i <= 4; i++) {
docs.push({ _id: i, a: i });
}
res = await client.db('test').collection('foo').insertMany(docs);
assert.equal(res.insertedCount, 4);
const actual = await client.db('test').collection('foo').findOne({ a: 4 });
assert.equal(actual.a, 4, 'Value should be 4');
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run()