File tree Expand file tree Collapse file tree 1 file changed +8
-3
lines changed
source/code-snippets/indexes Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change
1
+ // Create a multikey index
2
+
1
3
const { MongoClient } = require ( "mongodb" ) ;
2
4
3
- // Replace the following with your MongoDB deployment's connection
4
- // string.
5
+ // Replace the placeholders with your credentials
5
6
const uri =
6
7
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority" ;
7
8
@@ -13,23 +14,27 @@ async function run() {
13
14
const database = client . db ( "sample_mflix" ) ;
14
15
const movies = database . collection ( "movies" ) ;
15
16
16
- // Create a multikey index on the "cast" field
17
+ // Create a multikey index on the "cast" field in the "movies" collection
17
18
const result = await movies . createIndex ( { cast : 1 } ) ;
18
19
// end-idx
19
20
21
+ // Print the result of creating the index
20
22
console . log ( `Index created: ${ result } ` ) ;
21
23
22
24
// begin-query
23
25
const query = { cast : "Viola Davis" } ;
24
26
const projection = { _id : 0 , cast : 1 , title : 1 } ;
25
27
28
+ // Perform a find operation with the preceding filter and projection
26
29
const cursor = movies
27
30
. find ( query )
28
31
. project ( projection ) ;
29
32
// end-query
30
33
31
34
} finally {
35
+ // Close the connection after the operation completes
32
36
await client . close ( ) ;
33
37
}
34
38
}
39
+ // Run the program and print any errors
35
40
run ( ) . catch ( console . dir ) ;
You can’t perform that action at this time.
0 commit comments