Skip to content

DOCSP-30756-listIndexes-v5.5-and-earlier #705

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
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions source/code-snippets/indexes/listIndexes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { MongoClient } = require("mongodb");

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

const client = new MongoClient(uri);

async function run(){
try {
const database = client.db("<database>");
const collection = database.collection("<collection>");
// start-listIndexes-example
const result = await collection.listIndexes().toArray();
console.log("Existing indexes:\n");
for(const doc in result){
console.log(doc);
}
// end-listIndexes-example
}
finally{
await client.close();
}
}


19 changes: 19 additions & 0 deletions source/fundamentals/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,22 @@ the following:
E11000 duplicate key error index

To learn more, see :manual:`Unique Indexes </core/index-unique>`.

List Indexes
------------

You can use the ``listIndexes()`` method to list all of the indexes for
a collection. The `listIndexes()
<{+api+}/classes/Collection.html#listIndexes>`__ method takes an optional
`ListIndexesOptions <{+api+}/interfaces/ListIndexesOptions.html>`__
parameter. The ``listIndexes()`` method returns an object of type
`ListIndexesCursor <{+api+}/classes/ListIndexesCursor.html>`__.

The following code uses the ``listIndexes()`` method to list all the
indexes in a collection:

.. literalinclude:: /code-snippets/indexes/listIndexes.js
:language: js
:start-after: start-listIndexes-example
:end-before: end-listIndexes-example
:dedent: