File tree Expand file tree Collapse file tree 1 file changed +9
-3
lines changed
source/code-snippets/indexes Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change 1+ // List indexes
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
89const client = new MongoClient ( uri ) ;
910
10- import { MongoClient } from "mongodb" ;
11+ // Access a collection from a database
1112const database = client . db ( "<databaseName>" ) ;
1213const collection = database . collection ( "<collectionName>" ) ;
1314
1415async function run ( ) {
1516 try {
1617 // start listIndexes example
18+ // List the indexes on the collection and output them as an array
1719 const result = await collection . listIndexes ( ) . toArray ( ) ;
20+
21+ // Print the list of indexes
1822 console . log ( "Existing indexes:\n" ) ;
1923 for ( const doc in result ) {
2024 console . log ( doc ) ;
2125 }
2226 // end listIndexes example
2327 } finally {
28+ // Close the connection after the operation completes
2429 await client . close ( ) ;
2530 }
2631}
32+ // Run the program and print any errors
2733run ( ) . catch ( console . dir ) ;
You can’t perform that action at this time.
0 commit comments