Skip to content

DOCSP-32718: listindexes code comments #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions source/code-snippets/indexes/listIndexes.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
// List indexes

const { MongoClient } = require("mongodb");

// Replace the following with your MongoDB deployment's connection
// string.
// Replace the placeholders with your credentials
const uri =
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";

const client = new MongoClient(uri);

import { MongoClient } from "mongodb";
// Access a collection from a database
const database = client.db("<databaseName>");
const collection = database.collection("<collectionName>");

async function run() {
try {
// start listIndexes example
// List the indexes on the collection and output them as an array
const result = await collection.listIndexes().toArray();

// Print the list of indexes
console.log("Existing indexes:\n");
for(const doc in result){
console.log(doc);
}
// end listIndexes example
} finally {
// Close the connection after the operation completes
await client.close();
}
}
// Run the program and print any errors
run().catch(console.dir);