Skip to content

DOCSP-30756: add list search indexes to cursor page #704

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);

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

async function run() {
try {
// 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();
}
}
run().catch(console.dir);
27 changes: 27 additions & 0 deletions source/code-snippets/indexes/listSearchIndexes.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);

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

async function run() {
try {
// start listSearchIndexes example
const result = await collection.listSearchIndexes().toArray();
console.log("Existing search indexes:\n");
for(const doc in result){
console.log(doc);
}
// end listSearchIndexes example
} finally {
await client.close();
}
}
run().catch(console.dir);
4 changes: 3 additions & 1 deletion source/fundamentals/crud/read-operations/cursor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ The following functions directly return cursors:

- ``Collection.listIndexes()``

- ``Collection.listSearchIndexes()``

- ``Db.aggregate()``

- ``Db.listCollections()``
Expand Down Expand Up @@ -113,7 +115,7 @@ through results rather than returning all documents at once.
:language: javascript
:start-after: start fetchAll cursor example
:end-before: end fetchAll cursor example

Stream API
~~~~~~~~~~

Expand Down
40 changes: 40 additions & 0 deletions source/fundamentals/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,43 @@ 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: link to API docs for listIndexes() somewhere. same for below

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: javascript
:dedent:
:start-after: start listIndexes example
:end-before: end listIndexes example

List Search Indexes
~~~~~~~~~~~~~~~~~~~

When connecting to {+mdb-server+} version 7.0 or later, you can use the new `listSearchIndexes()
<https://mongodb.github.io/node-mongodb-native/Next/classes/Collection.html#listSearchIndexes>`__
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Investigating why the 5.6 docs don't cover this method

method to return a cursor that contains the search indexes of a given
collection. The ``listSearchIndexes()`` method takes an optional string parameter, ``name``, to
return only the indexes with matching index names. It also takes an
optional `aggregateOptions
<https://mongodb.github.io/node-mongodb-native/Next/interfaces/AggregateOptions.html>`__
parameter.

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

.. literalinclude:: /code-snippets/indexes/listSearchIndexes.js
:language: javascript
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add :dedent:

:dedent:
:start-after: start listSearchIndexes example
:end-before: end listSearchIndexes example