-
Notifications
You must be signed in to change notification settings - Fork 1
/
insert-data.js
44 lines (35 loc) · 1.72 KB
/
insert-data.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
const { MongoClient } = require("mongodb");
// Replace the following with your Atlas connection string
const url = "";
const client = new MongoClient(url);
// The database to use
const dbName = "test";
async function run() {
try {
await client.connect();
console.log("Connected correctly to server");
const db = client.db(dbName);
// Use the collection "people"
const col = db.collection("people");
// Construct a document
let personDocument = {
"name": { "first": "Alan", "last": "Turing" },
"birth": new Date(1912, 5, 23), // June 23, 1912
"death": new Date(1954, 5, 7), // June 7, 1954
"contribs": ["Turing machine", "Turing test", "Turingery"],
"views": 1250000
}
// Insert a single document, wait for promise so we can read it back
const p = await col.insertOne(personDocument);
// Find one document
const myDoc = await col.findOne();
// Print to the console
console.log(myDoc);
} catch (err) {
console.log(err.stack);
}
finally {
await client.close();
}
}
run().catch(console.dir);