Skip to content

Commit 9b22735

Browse files
(DOCSP 17936) Find TS (#240)
Co-authored-by: Chris Bush <[email protected]>
1 parent 2bac862 commit 9b22735

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function run() {
1919
const options = {
2020
// sort returned documents in ascending order by title (A->Z)
2121
sort: { title: 1 },
22-
// Include only the `title` and `imdb` fields in each returned document
22+
// Include only the `title` and `imdb` fields in each returned document
2323
projection: { _id: 0, title: 1, imdb: 1 },
2424
};
2525

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
type Minutes = number;
10+
11+
interface IMDB {
12+
rating: number;
13+
votes: number;
14+
id: number;
15+
}
16+
17+
interface Movie {
18+
title: string;
19+
imdb: IMDB;
20+
runtime: Minutes;
21+
}
22+
23+
async function run() {
24+
try {
25+
await client.connect();
26+
27+
const database = client.db("sample_mflix");
28+
const movies = database.collection<Movie>("movies");
29+
30+
const cursor = movies.find<Movie>(
31+
{ runtime: { $lt: 15 } },
32+
{
33+
sort: { title: 1 },
34+
projection: { _id: 0, title: 1, imdb: 1 },
35+
}
36+
);
37+
38+
if ((await cursor.count()) === 0) {
39+
console.warn("No documents found!");
40+
}
41+
42+
await cursor.forEach(console.dir);
43+
} finally {
44+
await client.close();
45+
}
46+
}
47+
run().catch(console.dir);

source/usage-examples/find.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,22 @@ uses the following parameters:
5050

5151
.. include:: /includes/connect-guide-note.rst
5252

53-
.. literalinclude:: /code-snippets/usage-examples/find.js
54-
:language: javascript
53+
54+
.. tabs::
55+
56+
.. tab:: JavaScript
57+
:tabid: javascript
58+
59+
.. literalinclude:: /code-snippets/usage-examples/find.js
60+
:language: javascript
61+
:linenos:
62+
63+
.. tab:: TypeScript
64+
:tabid: typescript
65+
66+
.. literalinclude:: /code-snippets/usage-examples/find.ts
67+
:language: typescript
68+
:linenos:
5569

5670
If you run the example above, you should see results that resemble the
5771
following:

0 commit comments

Comments
 (0)