Get a cursor over the index documents for this collection. >>> for index in db.test.list_indexes(): ... print(index) ... SON([('v', 2), ('key', SON([('_id', 1)])), ('name', '_id_')]) Cursors are closed automatically when they are exhausted (the l
(
self,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
)
| 2506 | self.database.client._retryable_write(False, inner, session, _Op.DROP_INDEXES) |
| 2507 | |
| 2508 | def list_indexes( |
| 2509 | self, |
| 2510 | session: Optional[ClientSession] = None, |
| 2511 | comment: Optional[Any] = None, |
| 2512 | ) -> CommandCursor[MutableMapping[str, Any]]: |
| 2513 | """Get a cursor over the index documents for this collection. |
| 2514 | |
| 2515 | >>> for index in db.test.list_indexes(): |
| 2516 | ... print(index) |
| 2517 | ... |
| 2518 | SON([('v', 2), ('key', SON([('_id', 1)])), ('name', '_id_')]) |
| 2519 | |
| 2520 | Cursors are closed automatically when they are exhausted (the last batch of data is retrieved from the database). |
| 2521 | If a cursor is not exhausted, it will be closed automatically upon garbage collection, which leaves resources open but unused for a potentially long period of time. |
| 2522 | To avoid this, best practice is to call :meth:`Cursor.close` when the cursor is no longer needed, |
| 2523 | or use the cursor in a with statement:: |
| 2524 | |
| 2525 | with collection.list_indexes() as cursor: |
| 2526 | for index in cursor: |
| 2527 | print(index) |
| 2528 | |
| 2529 | :param session: a |
| 2530 | :class:`~pymongo.client_session.ClientSession`. |
| 2531 | :param comment: A user-provided comment to attach to this |
| 2532 | command. |
| 2533 | |
| 2534 | :return: An instance of :class:`~pymongo.command_cursor.CommandCursor`. |
| 2535 | |
| 2536 | .. versionchanged:: 4.1 |
| 2537 | Added ``comment`` parameter. |
| 2538 | |
| 2539 | .. versionchanged:: 3.6 |
| 2540 | Added ``session`` parameter. |
| 2541 | |
| 2542 | .. versionadded:: 3.0 |
| 2543 | """ |
| 2544 | return self._list_indexes(session, comment) |
| 2545 | |
| 2546 | def _list_indexes( |
| 2547 | self, |