@@ -15,54 +15,60 @@ Definition
1515
1616.. dbcommand:: listIndexes
1717
18- Returns information about the indexes on the specified collection,
19- including :doc:`hidden indexes </core/index-hidden>`. Specifically,
20- the command returns a document that contains information with which
21- to create a cursor to the index information. Index information
22- includes the keys and options used to create the index. The
18+ Returns information about the indexes on the specified collection. Returned
19+ index information includes the keys and options used to create the index, as
20+ well as :doc:`hidden indexes </core/index-hidden>`. You can optionally set
21+ the batch size for the first batch of results. The
2322 :binary:`~bin.mongo` shell provides the
2423 :method:`db.collection.getIndexes()` helper.
2524
26- The command has the following form:
25+ Syntax
26+ ------
2727
28- .. code-block:: javascript
28+ The command has the following form:
2929
30- { "listIndexes": "<collection-name>" }
30+ .. code-block:: javascript
31+
32+ db.runCommand (
33+ {
34+ listIndexes: "<collection-name>",
35+ cursor: { batchSize: <int> },
36+ comment: <any>
37+ }
38+ )
3139
40+ Command Fields
41+ ~~~~~~~~~~~~~~
3242
33- .. list-table::
34- :header-rows: 1
35- :widths: 20 20 80
36-
37- * - Field
38-
39- - Type
40-
41- - Description
42-
43- * - ``listIndexes``
43+ ``listIndexes`` takes the following fields:
44+
45+ .. list-table::
46+ :header-rows: 1
47+ :widths: 20 20 80
4448
45- - string
49+ * - Field
50+ - Type
51+ - Description
4652
47- - The name of the collection.
48-
49- * - ``comment``
50-
51- - any
52-
53- - .. include:: /includes/extracts/comment-content.rst
54-
55- .. versionadded:: 4.4
56-
57-
53+ * - ``listIndexes``
54+ - string
55+ - The name of the collection.
56+
57+ * - ``cursor.batchSize``
58+ - integer
59+ - Optional. Specifies the cursor batch size.
60+
61+ * - ``comment``
62+ - any
63+ - .. include:: /includes/extracts/comment-content.rst
64+ .. versionadded:: 4.4
5865
5966Required Access
6067---------------
6168
62- To run :dbcommand:`listIndexes` when access control is enforced, users
63- must have privileges to :authaction:`listIndexes`. The built-in role
64- :authrole:`read` provides the required privileges to run
65- :dbcommand:`listIndexes` for the collections in a database.
69+ If access control is enforced, the built-in :authrole:`read` role provides the
70+ required privileges to run :dbcommand:`listIndexes` for the collections in a
71+ database.
6672
6773Behavior
6874--------
@@ -86,17 +92,138 @@ Output
8692
8793.. data:: listIndexes.cursor
8894
89- A document that contains information with which to create a cursor
90- to index information. The cursor information includes the cursor id,
91- the full namespace for the command, as well as the first batch of
92- results. Index information includes the keys and options used to
93- create the index. The index option ``hidden``, available starting in
94- MongoDB 4.4, is only present if the value is ``true``.
95-
96- For information on the keys and index options, see
97- :method:`db.collection.createIndex()`.
95+ A result set returned in the batch size specified by your cursor.
96+ Each document in the batch output contains the following fields:
97+
98+ .. list-table::
99+ :header-rows: 1
100+ :widths: 15 15 30
101+
102+ * - Field
103+ - Type
104+ - Description
105+
106+ * - id
107+ - integer
108+ - A 64-bit integer. If zero, there are no more batches of information.
109+ If non-zero, a cursor ID, usable in a ``getMore`` command to get the
110+ next batch of index information.
111+
112+ * - ns
113+ - string
114+ - The database and collection name in the following format:
115+ ``<database-name>.<collection-name>``
116+
117+ * - firstBatch
118+ - document
119+ - Index information includes the keys and options used to create the
120+ index. The index option hidden, available starting in MongoDB 4.4,
121+ is only present if the value is true.
122+
123+ Use :dbcommand:`getMore` to retrieve additional results as needed.
124+
98125
99126.. data:: listIndexes.ok
100127
101- The return value for the command. A value of ``1`` indicates
102- success.
128+ The return value for the command. A value of ``1`` indicates success.
129+
130+ Examples
131+ --------
132+
133+ List Database Indexes
134+ ~~~~~~~~~~~~~~~~~~~~~
135+
136+ This example lists indexes for the ``contacts`` collection without specifying the
137+ cursor batch size.
138+
139+ .. io-code-block::
140+ :copyable: true
141+
142+ .. input::
143+ :language: json
144+ :linenos:
145+
146+ db.runCommand (
147+ {
148+ listIndexes: "contacts"
149+ }
150+ )
151+
152+ .. output::
153+ :linenos:
154+
155+ {
156+ cursor: {
157+ id: Long("0"),
158+ ns: 'test.contacts',
159+ firstBatch: [
160+ { v: 2, key: { _id: 1 }, name: '_id_', ns: 'test.contacts' },
161+ { v: 2, key: { a: 1 }, name: 'a_1', ns: 'test.contacts' }
162+ ]
163+ },
164+ ok: 1
165+ }
166+
167+ Specify Result Batch Size
168+ ~~~~~~~~~~~~~~~~~~~~~~~~~
169+
170+ This example lists indexes for the ``contacts`` collection, and specifies a cursor
171+ batch size of 1.
172+
173+ .. io-code-block::
174+ :copyable: true
175+
176+ .. input::
177+ :language: json
178+ :linenos:
179+
180+ db.runCommand (
181+ {
182+ listIndexes: "contacts", cursor: { batchSize: 1 }
183+ }
184+ )
185+
186+ .. output::
187+ :linenos:
188+
189+ {
190+ cursor: {
191+ id: Long("4809221676960028307"),
192+ ns: 'test.contacts',
193+ firstBatch: [ { v: 2, key: { _id: 1 }, name: '_id_', ns: 'test.contacts' } ]
194+ },
195+ ok: 1
196+ }
197+
198+ Retrieve Additional Results
199+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
200+
201+ This example uses ``getMore`` to retrieve additional result batches from the
202+ ``contacts`` collection.
203+
204+ .. io-code-block::
205+ :copyable: true
206+
207+ .. input::
208+ :language: json
209+ :linenos:
210+
211+ db.runCommand(
212+ {
213+ getMore: Long("4809221676960028307"), collection: "contacts"
214+ }
215+ )
216+
217+ .. output::
218+ :linenos:
219+
220+ {
221+ cursor: {
222+ nextBatch: [ { v: 2, key: { a: 1 }, name: 'a_1', ns: 'test.contacts' } ],
223+ id: Long("0"),
224+ ns: 'test.contacts'
225+ },
226+ ok: 1
227+ }
228+
229+
0 commit comments