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
+
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
8
9
const client = new MongoClient ( uri ) ;
9
10
10
- import { MongoClient } from "mongodb" ;
11
+ // Access a collection from a database
11
12
const database = client . db ( "<databaseName>" ) ;
12
13
const collection = database . collection ( "<collectionName>" ) ;
13
14
14
15
async function run ( ) {
15
16
try {
16
17
// start listIndexes example
18
+ // List the indexes on the collection and output them as an array
17
19
const result = await collection . listIndexes ( ) . toArray ( ) ;
20
+
21
+ // Print the list of indexes
18
22
console . log ( "Existing indexes:\n" ) ;
19
23
for ( const doc in result ) {
20
24
console . log ( doc ) ;
21
25
}
22
26
// end listIndexes example
23
27
} finally {
28
+ // Close the connection after the operation completes
24
29
await client . close ( ) ;
25
30
}
26
31
}
32
+ // Run the program and print any errors
27
33
run ( ) . catch ( console . dir ) ;
You can’t perform that action at this time.
0 commit comments