File tree Expand file tree Collapse file tree 3 files changed +64
-3
lines changed
code-snippets/usage-examples Expand file tree Collapse file tree 3 files changed +64
-3
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff 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
5670If you run the example above, you should see results that resemble the
5771following:
You can’t perform that action at this time.
0 commit comments