File tree Expand file tree Collapse file tree 7 files changed +223
-140
lines changed Expand file tree Collapse file tree 7 files changed +223
-140
lines changed Original file line number Diff line number Diff line change @@ -11,20 +11,27 @@ async function run() {
1111 try {
1212 await client . connect ( ) ;
1313
14- // begin-ex
15- //
16- // (Connection code omitted for space.)
17- //
18- // Specify the "sample_mflix" database.
19- const mflix = client . db ( "sample_mflix" ) ;
14+ // begin-idx
15+ const db = client . db ( "sample_mflix" ) ;
16+ const movies = db . collection ( "movies" ) ;
2017
2118 // Create an ascending index on the "type" and "genre" fields
2219 // in the "movies" collection.
23- const result = await mflix
24- . collection ( "movies" )
25- . createIndex ( { type : 1 , genre : 1 } ) ;
26- console . log ( "Index " + result + " created." ) ;
27- // end-example
20+ const result = await movies . createIndex ( { type : 1 , genre : 1 } ) ;
21+ console . log ( `Index created: ${ result } ` ) ;
22+ // end-idx
23+
24+ // begin-query
25+ const query = { type : "movie" , genre : "Drama" } ;
26+ const sort = { type : 1 , genre : 1 } ;
27+ const projection = { type : 1 , genre : 1 } ;
28+
29+ const cursor = movies
30+ . find ( query )
31+ . sort ( sort )
32+ . project ( projection ) ;
33+ // end-query
34+
2835 } finally {
2936 await client . close ( ) ;
3037 }
Original file line number Diff line number Diff line change @@ -11,20 +11,14 @@ async function run() {
1111 try {
1212 await client . connect ( ) ;
1313
14- // begin-ex
15- //
16- // (Connection code omitted for space.)
17- //
18- // Specify the "sample_mflix" database.
19- const mflix = client . db ( "sample_mflix" ) ;
14+ // begin-idx
15+ const db = client . db ( "sample_mflix" ) ;
16+ const movies = db . collection ( "movies" ) ;
2017
21- // Create a 2dsphere index on the "location.geo" field in the
22- // "theaters" collection.
23- const result = await mflix
24- . collection ( "movies" )
25- . createIndex ( { "location.geo" : "2dsphere" } ) ;
26- console . log ( "Index " + result + " created." ) ;
27- // end-ex
18+ // Create a 2dsphere index on the "location.geo" field in the "theaters" collection.
19+ const result = await movies . createIndex ( { "location.geo" : "2dsphere" } ) ;
20+ console . log ( `Index created: ${ result } ` ) ;
21+ // end-idx
2822 } finally {
2923 await client . close ( ) ;
3024 }
Original file line number Diff line number Diff line change @@ -11,19 +11,26 @@ async function run() {
1111 try {
1212 await client . connect ( ) ;
1313
14- // begin-ex
15- //
16- // (Connection code omitted for space.)
17- //
18- // Specify the "sample_mflix" database.
19- const mflix = client . db ( "sample_mflix" ) ;
14+ // begin-idx
15+ const db = client . db ( "sample_mflix" ) ;
16+ const movies = db . collection ( "movies" ) ;
2017
2118 // Create a multikey index on the "cast" array field
2219 // in the "movies" collection.
23- const result = await mflix . collection ( "movies" ) . createIndex ( { cast : 1 } ) ;
24- console . log ( "Index " + result + " created." ) ;
20+ const result = await movies . createIndex ( { cast : 1 } ) ;
21+ console . log ( `Index created: ${ result } ` ) ;
22+ // end-idx
2523
26- // end-ex
24+ // begin-query
25+ const query = { cast : "Burt Reynolds" } ;
26+ const sort = { cast : 1 , genre : 1 } ;
27+ const projection = { cast : 1 } ;
28+
29+ const cursor = movies
30+ . find ( query )
31+ . sort ( sort )
32+ . project ( projection ) ;
33+ // end-query
2734 } finally {
2835 await client . close ( ) ;
2936 }
Original file line number Diff line number Diff line change @@ -11,19 +11,26 @@ async function run() {
1111 try {
1212 await client . connect ( ) ;
1313
14- // begin-ex
15- //
16- // (Connection code omitted for space.)
17- //
18- // Specify the "sample_mflix" database.
19- const mflix = client . db ( "sample_mflix" ) ;
14+ // begin-idx
15+ const db = client . db ( "sample_mflix" ) ;
16+ const movies = db . collection ( "movies" ) ;
2017
2118 // Create an ascending index on the "title" field in the
2219 // "movies" collection.
23- const result = await mflix . collection ( "movies" ) . createIndex ( { title : 1 } ) ;
24- console . log ( "Index " + result + " created." ) ;
20+ const result = await movies . createIndex ( { title : 1 } ) ;
21+ console . log ( `Index created: ${ result } ` ) ;
22+ // end-idx
2523
26- // end-ex
24+ // begin-query
25+ const query = { title : "Batman" }
26+ const sort = { title : 1 } ;
27+ const projection = { title : 1 } ;
28+
29+ const cursor = movies
30+ . find ( query )
31+ . sort ( sort )
32+ . project ( projection ) ;
33+ // end-query
2734 } finally {
2835 await client . close ( ) ;
2936 }
Original file line number Diff line number Diff line change @@ -11,20 +11,24 @@ async function run() {
1111 try {
1212 await client . connect ( ) ;
1313
14- // begin-ex
15- //
16- // (Connection code omitted for space.)
17- //
18- // Specify the "sample_mflix" database.
19- const mflix = client . db ( "sample_mflix" ) ;
14+ // begin-idx
15+ const db = client . db ( "sample_mflix" ) ;
16+ const movies = db . collection ( "movies" ) ;
2017
2118 // Create a text index on the "fullplot" field in the
2219 // "movies" collection.
23- const result = await mflix
24- . collection ( "movies" )
25- . createIndex ( { fullplot : "text" } , { default_language : "english" } ) ;
26- console . log ( "Index " + result + " created." ) ;
27- // end-ex
20+ const result = await movies . createIndex ( { fullplot : "text" } , { default_language : "english" } ) ;
21+ console . log ( `Index created: ${ result } ` ) ;
22+ // end-idx
23+
24+ // begin-query
25+ const query = { $text : { $search : "java coffee shop" } } ;
26+ const projection = { fullplot : 1 } ;
27+ const cursor = movies
28+ . find ( query )
29+ . projection ( projection ) ;
30+ // end-query
31+
2832 } finally {
2933 await client . close ( ) ;
3034 }
Original file line number Diff line number Diff line change @@ -11,21 +11,14 @@ async function run() {
1111 try {
1212 await client . connect ( ) ;
1313
14- // begin-ex
15- //
16- // (Connection code omitted for space.)
17- //
18- // Specify the "sample_mflix" database.
19- const mflix = client . db ( "sample_mflix" ) ;
14+ // begin-idx
15+ const db = client . db ( "sample_mflix" ) ;
16+ const movies = db . collection ( "movies" ) ;
2017
21- // Create an ascending unique index on the "theaterId" field in the
22- // "theaters" collection.
23- const result = await mflix
24- . collection ( "theaters" )
25- . createIndex ( { theaterId : 1 } , { unique : true } ) ;
26- console . log ( "Index " + result + " created." ) ;
27-
28- // end-ex
18+ // Create a unique index on the "theaterId" field in the "theaters" collection.
19+ const result = await movies . createIndex ( { theaterId : 1 } , { unique : true } ) ;
20+ console . log ( `Index created: ${ result } ` ) ;
21+ // end-idx
2922 } finally {
3023 await client . close ( ) ;
3124 }
You can’t perform that action at this time.
0 commit comments