Skip to content

Commit f150665

Browse files
committed
DOCSP-30756: add list search indexes to cursor page (#704)
* first draft * first draft * first draft * first draft * first draft * first draft v1.1 * big fixes * big fixes * review fixes (cherry picked from commit 966fe9f)
1 parent ae7687f commit f150665

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
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+
import { MongoClient } from "mongodb";
11+
const database = client.db("<databaseName>");
12+
const collection = database.collection("<collectionName>");
13+
14+
async function run() {
15+
try {
16+
// start listIndexes example
17+
const result = await collection.listIndexes().toArray();
18+
console.log("Existing indexes:\n");
19+
for(const doc in result){
20+
console.log(doc);
21+
}
22+
// end listIndexes example
23+
} finally {
24+
await client.close();
25+
}
26+
}
27+
run().catch(console.dir);
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+
import { MongoClient } from "mongodb";
11+
const database = client.db("<databaseName>");
12+
const collection = database.collection("<collectionName>");
13+
14+
async function run() {
15+
try {
16+
// start listSearchIndexes example
17+
const result = await collection.listSearchIndexes().toArray();
18+
console.log("Existing search indexes:\n");
19+
for(const doc in result){
20+
console.log(doc);
21+
}
22+
// end listSearchIndexes example
23+
} finally {
24+
await client.close();
25+
}
26+
}
27+
run().catch(console.dir);

source/fundamentals/crud/read-operations/cursor.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ The following functions directly return cursors:
3030

3131
- ``Collection.listIndexes()``
3232

33+
- ``Collection.listSearchIndexes()``
34+
3335
- ``Db.aggregate()``
3436

3537
- ``Db.listCollections()``
@@ -113,7 +115,7 @@ through results rather than returning all documents at once.
113115
:language: javascript
114116
:start-after: start fetchAll cursor example
115117
:end-before: end fetchAll cursor example
116-
118+
117119
Stream API
118120
~~~~~~~~~~
119121

source/fundamentals/indexes.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,43 @@ the following:
312312
E11000 duplicate key error index
313313

314314
To learn more, see :manual:`Unique Indexes </core/index-unique>`.
315+
316+
List Indexes
317+
------------
318+
319+
You can use the ``listIndexes()`` method to list all of the indexes
320+
for a collection. The `listIndexes() <{+api+}/classes/Collection.html#listIndexes>`__ method takes an
321+
optional `ListIndexesOptions
322+
<{+api+}/interfaces/ListIndexesOptions.html>`__ parameter. The ``listIndexes()`` method returns an
323+
object of type `ListIndexesCursor
324+
<{+api+}/classes/ListIndexesCursor.html>`__.
325+
326+
The following code uses the ``listIndexes()`` method to list all the
327+
indexes in a collection:
328+
329+
.. literalinclude:: /code-snippets/indexes/listIndexes.js
330+
:language: javascript
331+
:dedent:
332+
:start-after: start listIndexes example
333+
:end-before: end listIndexes example
334+
335+
List Search Indexes
336+
~~~~~~~~~~~~~~~~~~~
337+
338+
When connecting to {+mdb-server+} version 7.0 or later, you can use the new `listSearchIndexes()
339+
<https://mongodb.github.io/node-mongodb-native/Next/classes/Collection.html#listSearchIndexes>`__
340+
method to return a cursor that contains the search indexes of a given
341+
collection. The ``listSearchIndexes()`` method takes an optional string parameter, ``name``, to
342+
return only the indexes with matching index names. It also takes an
343+
optional `aggregateOptions
344+
<https://mongodb.github.io/node-mongodb-native/Next/interfaces/AggregateOptions.html>`__
345+
parameter.
346+
347+
The following code uses the ``listSearchIndexes()`` method to list all
348+
the search indexes in the collection:
349+
350+
.. literalinclude:: /code-snippets/indexes/listSearchIndexes.js
351+
:language: javascript
352+
:dedent:
353+
:start-after: start listSearchIndexes example
354+
:end-before: end listSearchIndexes example

0 commit comments

Comments
 (0)