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+
13const { MongoClient } = require ( "mongodb" ) ;
24
3- // Replace the following with your MongoDB deployment's connection
4- // string.
5+ // Replace the placeholders with your credentials
56const uri =
67 "mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority" ;
78
@@ -13,23 +14,27 @@ async function run() {
1314 const database = client . db ( "sample_mflix" ) ;
1415 const movies = database . collection ( "movies" ) ;
1516
16- // Create a multikey index on the "cast" field
17+ // Create a multikey index on the "cast" field in the "movies" collection
1718 const result = await movies . createIndex ( { cast : 1 } ) ;
1819 // end-idx
1920
21+ // Print the result of creating the index
2022 console . log ( `Index created: ${ result } ` ) ;
2123
2224 // begin-query
2325 const query = { cast : "Viola Davis" } ;
2426 const projection = { _id : 0 , cast : 1 , title : 1 } ;
2527
28+ // Perform a find operation with the preceding filter and projection
2629 const cursor = movies
2730 . find ( query )
2831 . project ( projection ) ;
2932 // end-query
3033
3134 } finally {
35+ // Close the connection after the operation completes
3236 await client . close ( ) ;
3337 }
3438}
39+ // Run the program and print any errors
3540run ( ) . catch ( console . dir ) ;
You can’t perform that action at this time.
0 commit comments