Get a cursor over the databases of the connected server. Cursors are closed automatically when they are exhausted (the last batch of data is retrieved from the database). If a cursor is not exhausted, it will be closed automatically upon garbage collection, which leaves resources op
(
self,
session: Optional[client_session.ClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
)
| 2377 | return CommandCursor(admin["$cmd"], cursor, None, comment=comment) |
| 2378 | |
| 2379 | def list_databases( |
| 2380 | self, |
| 2381 | session: Optional[client_session.ClientSession] = None, |
| 2382 | comment: Optional[Any] = None, |
| 2383 | **kwargs: Any, |
| 2384 | ) -> CommandCursor[dict[str, Any]]: |
| 2385 | """Get a cursor over the databases of the connected server. |
| 2386 | |
| 2387 | Cursors are closed automatically when they are exhausted (the last batch of data is retrieved from the database). |
| 2388 | 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. |
| 2389 | To avoid this, best practice is to call :meth:`Cursor.close` when the cursor is no longer needed, |
| 2390 | or use the cursor in a with statement:: |
| 2391 | |
| 2392 | with client.list_databases() as cursor: |
| 2393 | for database in cursor: |
| 2394 | print(database) |
| 2395 | |
| 2396 | :param session: a |
| 2397 | :class:`~pymongo.client_session.ClientSession`. |
| 2398 | :param comment: A user-provided comment to attach to this |
| 2399 | command. |
| 2400 | :param kwargs: Optional parameters of the |
| 2401 | `listDatabases command |
| 2402 | <https://mongodb.com/docs/manual/reference/command/listDatabases/>`_ |
| 2403 | can be passed as keyword arguments to this method. The supported |
| 2404 | options differ by server version. |
| 2405 | |
| 2406 | |
| 2407 | :return: An instance of :class:`~pymongo.command_cursor.CommandCursor`. |
| 2408 | |
| 2409 | .. versionadded:: 3.6 |
| 2410 | """ |
| 2411 | return self._list_databases(session, comment, **kwargs) |
| 2412 | |
| 2413 | def list_database_names( |
| 2414 | self, |