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() {
11
11
try {
12
12
await client . connect ( ) ;
13
13
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" ) ;
20
17
21
18
// Create an ascending index on the "type" and "genre" fields
22
19
// 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
+
28
35
} finally {
29
36
await client . close ( ) ;
30
37
}
Original file line number Diff line number Diff line change @@ -11,20 +11,14 @@ async function run() {
11
11
try {
12
12
await client . connect ( ) ;
13
13
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" ) ;
20
17
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
28
22
} finally {
29
23
await client . close ( ) ;
30
24
}
Original file line number Diff line number Diff line change @@ -11,19 +11,26 @@ async function run() {
11
11
try {
12
12
await client . connect ( ) ;
13
13
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" ) ;
20
17
21
18
// Create a multikey index on the "cast" array field
22
19
// 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
25
23
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
27
34
} finally {
28
35
await client . close ( ) ;
29
36
}
Original file line number Diff line number Diff line change @@ -11,19 +11,26 @@ async function run() {
11
11
try {
12
12
await client . connect ( ) ;
13
13
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" ) ;
20
17
21
18
// Create an ascending index on the "title" field in the
22
19
// "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
25
23
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
27
34
} finally {
28
35
await client . close ( ) ;
29
36
}
Original file line number Diff line number Diff line change @@ -11,20 +11,24 @@ async function run() {
11
11
try {
12
12
await client . connect ( ) ;
13
13
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" ) ;
20
17
21
18
// Create a text index on the "fullplot" field in the
22
19
// "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
+
28
32
} finally {
29
33
await client . close ( ) ;
30
34
}
Original file line number Diff line number Diff line change @@ -11,21 +11,14 @@ async function run() {
11
11
try {
12
12
await client . connect ( ) ;
13
13
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" ) ;
20
17
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
29
22
} finally {
30
23
await client . close ( ) ;
31
24
}
You can’t perform that action at this time.
0 commit comments