Skip to content

Commit 8dd90e3

Browse files
DOCSP-17936 add TS Insert Many usage example (#227)
1 parent 40adc24 commit 8dd90e3

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

source/code-snippets/usage-examples/insertMany.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ async function run() {
1010
try {
1111
await client.connect();
1212

13-
const database = client.db("sample_mflix");
14-
const movies = database.collection("movies");
13+
const database = client.db("insert_db");
14+
const foods = database.collection("foods");
1515

1616
// create an array of documents to insert
1717
const docs = [
18-
{ name: "Red", town: "Kanto" },
19-
{ name: "Blue", town: "Kanto" },
20-
{ name: "Leon", town: "Galar" }
18+
{ name: "cake", healthy: false },
19+
{ name: "lettuce", healthy: true },
20+
{ name: "donut", healthy: false }
2121
];
2222

2323
// this option prevents additional documents from being inserted if one fails
2424
const options = { ordered: true };
2525

26-
const result = await movies.insertMany(docs, options);
26+
const result = await foods.insertMany(docs, options);
2727
console.log(`${result.insertedCount} documents were inserted`);
2828
} finally {
2929
await client.close();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { MongoClient } from "mongodb";
2+
3+
// Replace the uri string with your MongoDB deployment's connection string.
4+
const uri =
5+
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
6+
7+
const client = new MongoClient(uri);
8+
9+
interface Food {
10+
name: string;
11+
healthy: boolean;
12+
}
13+
14+
async function run() {
15+
try {
16+
await client.connect();
17+
18+
const database = client.db("insert_db");
19+
// Specifying a schema is optional, but it enables type hints on
20+
// finds and inserts
21+
const foods = database.collection<Food>("foods");
22+
23+
const result = await foods.insertMany(
24+
[
25+
{ name: "cake", healthy: false },
26+
{ name: "lettuce", healthy: true },
27+
{ name: "donut", healthy: false },
28+
],
29+
{ ordered: true }
30+
);
31+
console.log(`${result.insertedCount} documents were inserted`);
32+
} finally {
33+
await client.close();
34+
}
35+
}
36+
run().catch(console.dir);

source/usage-examples/insertMany.txt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ Example
3030

3131
.. include:: /includes/connect-guide-note.rst
3232

33-
.. literalinclude:: /code-snippets/usage-examples/insertMany.js
34-
:language: javascript
35-
:linenos:
33+
.. tabs::
34+
35+
.. tab:: JavaScript
36+
:tabid: javascript
37+
38+
.. literalinclude:: /code-snippets/usage-examples/insertMany.js
39+
:language: javascript
40+
:linenos:
41+
42+
.. tab:: TypeScript
43+
:tabid: typescript
44+
45+
.. literalinclude:: /code-snippets/usage-examples/insertMany.ts
46+
:language: typescript
47+
:linenos:
48+
49+
If you run the example above, you should see output that resembles the following:
50+
51+
.. code-block:: none
52+
53+
3 documents were inserted

0 commit comments

Comments
 (0)