File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed
source/code-snippets/usage-examples Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -3,22 +3,29 @@ import { MongoClient } from "mongodb";
3
3
// Replace the uri string with your MongoDB deployment's connection string.
4
4
const uri = "<connection string uri>" ;
5
5
6
+ // Create a new client and connect to MongoDB
6
7
const client = new MongoClient ( uri ) ;
7
8
8
9
async function run ( ) {
9
10
try {
11
+ // Connect to the "insertDB" database and access its "haiku" collection
10
12
const database = client . db ( "insertDB" ) ;
11
13
const haiku = database . collection ( "haiku" ) ;
12
- // create a document to insert
14
+
15
+ // Create a document to insert
13
16
const doc = {
14
17
title : "Record of a Shriveled Datum" ,
15
18
content : "No bytes, no problem. Just insert a document, in MongoDB" ,
16
19
}
20
+ // Insert the defined document into the "haiku" collection
17
21
const result = await haiku . insertOne ( doc ) ;
18
22
23
+ // Print the ID of the inserted document
19
24
console . log ( `A document was inserted with the _id: ${ result . insertedId } ` ) ;
20
25
} finally {
26
+ // Close the MongoDB client connection
21
27
await client . close ( ) ;
22
28
}
23
29
}
30
+ // Run the function and handle any errors
24
31
run ( ) . catch ( console . dir ) ;
You can’t perform that action at this time.
0 commit comments