Skip to content

Commit 9f8387d

Browse files
Docsp 17936 insert one type script example (#225)
Co-authored-by: Nathan <[email protected]>
1 parent e8b7348 commit 9f8387d

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

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

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

13-
const database = client.db("sample_mflix");
14-
const movies = database.collection("movies");
15-
// create a document to be inserted
16-
const doc = { name: "Red", town: "kanto" };
17-
const result = await movies.insertOne(doc);
13+
const database = client.db("insert_db");
14+
const haiku = database.collection("haiku");
15+
// create a document to insert
16+
const doc = {
17+
title: "Record of a Shriveled Datum",
18+
content: "No bytes, no problem. Just insert a document, in MongoDB",
19+
}
20+
const result = await haiku.insertOne(doc);
1821

19-
console.log(
20-
`${result.insertedCount} documents were inserted with the _id: ${result.insertedId}`,
21-
);
22+
console.log(`A document was inserted with the _id: ${result.insertedId}`);
2223
} finally {
2324
await client.close();
2425
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 Haiku {
10+
title: string;
11+
content: string;
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 haiku = database.collection<Haiku>("haiku");
22+
const result = await haiku.insertOne({
23+
title: "Record of a Shriveled Datum",
24+
content: "No bytes, no problem. Just insert a document, in MongoDB",
25+
});
26+
console.log(`A document was inserted with the _id: ${result.insertedId}`);
27+
} finally {
28+
await client.close();
29+
}
30+
}
31+
run().catch(console.dir);

source/usage-examples/insertOne.txt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ Example
3636

3737
.. include:: /includes/connect-guide-note.rst
3838

39-
.. literalinclude:: /code-snippets/usage-examples/insertOne.js
40-
:language: javascript
41-
:linenos:
39+
.. tabs::
40+
41+
.. tab:: JavaScript
42+
:tabid: javascript
43+
44+
.. literalinclude:: /code-snippets/usage-examples/insertOne.js
45+
:language: javascript
46+
:linenos:
47+
48+
.. tab:: TypeScript
49+
:tabid: typescript
50+
51+
.. literalinclude:: /code-snippets/usage-examples/insertOne.ts
52+
:language: typescript
53+
:linenos:
54+
55+
If you run the example above, you should see output that resembles the following:
56+
57+
.. code-block:: none
58+
59+
A document was inserted with the _id: <your _id value>
60+

0 commit comments

Comments
 (0)