Skip to content

Commit 04f30f4

Browse files
committed
Merge branch 'v3.6' of github.com:mongodb/docs-node into v3.6
2 parents e747538 + ce50164 commit 04f30f4

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { MongoClient } = require("mongodb");
2+
3+
// Replace the following with your MongoDB deployment's connection
4+
// string.
5+
const uri =
6+
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
7+
8+
const client = new MongoClient(uri);
9+
10+
async function run(){
11+
try {
12+
const database = client.db("<database>");
13+
const collection = database.collection("<collection>");
14+
// start-listIndexes-example
15+
const result = await collection.listIndexes().toArray();
16+
console.log("Existing indexes:\n");
17+
for(const doc in result){
18+
console.log(doc);
19+
}
20+
// end-listIndexes-example
21+
}
22+
finally{
23+
await client.close();
24+
}
25+
}
26+
27+

source/fundamentals/indexes.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,22 @@ the following:
271271

272272
Refer to the :manual:`Unique Indexes page </core/index-unique>` in the
273273
MongoDB server manual for more information.
274+
275+
List Indexes
276+
------------
277+
278+
You can use the ``listIndexes()`` method to list all of the indexes for
279+
a collection. The `listIndexes()
280+
<{+api+}/classes/Collection.html#listIndexes>`__ method takes an optional
281+
`ListIndexesOptions <{+api+}/interfaces/ListIndexesOptions.html>`__
282+
parameter. The ``listIndexes()`` method returns an object of type
283+
`ListIndexesCursor <{+api+}/classes/ListIndexesCursor.html>`__.
284+
285+
The following code uses the ``listIndexes()`` method to list all the
286+
indexes in a collection:
287+
288+
.. literalinclude:: /code-snippets/indexes/listIndexes.js
289+
:language: js
290+
:start-after: start-listIndexes-example
291+
:end-before: end-listIndexes-example
292+
:dedent:

0 commit comments

Comments
 (0)