forked from mongodb/docs-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertOne.ts
32 lines (28 loc) · 875 Bytes
/
insertOne.ts
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
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.
const uri = "<connection string uri>";
const client = new MongoClient(uri);
interface Movie {
title: string;
content: string[];
year: number;
cast: string[];
}
async function run() {
try {
const database = client.db("sample_mflix");
// Specifying a Schema is optional, but it enables type hints on
// finds and inserts
const movies = database.collection<Movie>("movies");
const result = await movies.insertOne({
title: "Charade",
genres: ["Comedy", "Romance", "Thriller"],
year: 1963,
cast: ["Cary Grant", "Audrey Hepburn", "Walter Matthau"],
});
console.log(`A document was inserted with the _id: ${result.insertedId}`);
} finally {
await client.close();
}
}
run().catch(console.dir);