Get a :class:`~pymongo.database.Database` with the given name and options. Useful for creating a :class:`~pymongo.database.Database` with different codec options, read preference, and/or write concern from this :class:`MongoClient`. >>> client.read_prefere
(
self,
name: Optional[str] = None,
codec_options: Optional[CodecOptions[_DocumentTypeArg]] = None,
read_preference: Optional[_ServerMode] = None,
write_concern: Optional[WriteConcern] = None,
read_concern: Optional[ReadConcern] = None,
)
| 1512 | ) |
| 1513 | |
| 1514 | def get_database( |
| 1515 | self, |
| 1516 | name: Optional[str] = None, |
| 1517 | codec_options: Optional[CodecOptions[_DocumentTypeArg]] = None, |
| 1518 | read_preference: Optional[_ServerMode] = None, |
| 1519 | write_concern: Optional[WriteConcern] = None, |
| 1520 | read_concern: Optional[ReadConcern] = None, |
| 1521 | ) -> database.Database[_DocumentType]: |
| 1522 | """Get a :class:`~pymongo.database.Database` with the given name and |
| 1523 | options. |
| 1524 | |
| 1525 | Useful for creating a :class:`~pymongo.database.Database` with |
| 1526 | different codec options, read preference, and/or write concern from |
| 1527 | this :class:`MongoClient`. |
| 1528 | |
| 1529 | >>> client.read_preference |
| 1530 | Primary() |
| 1531 | >>> db1 = client.test |
| 1532 | >>> db1.read_preference |
| 1533 | Primary() |
| 1534 | >>> from pymongo import ReadPreference |
| 1535 | >>> db2 = client.get_database( |
| 1536 | ... 'test', read_preference=ReadPreference.SECONDARY) |
| 1537 | >>> db2.read_preference |
| 1538 | Secondary(tag_sets=None) |
| 1539 | |
| 1540 | :param name: The name of the database - a string. If ``None`` |
| 1541 | (the default) the database named in the MongoDB connection URI is |
| 1542 | returned. |
| 1543 | :param codec_options: An instance of |
| 1544 | :class:`~bson.codec_options.CodecOptions`. If ``None`` (the |
| 1545 | default) the :attr:`codec_options` of this :class:`MongoClient` is |
| 1546 | used. |
| 1547 | :param read_preference: The read preference to use. If |
| 1548 | ``None`` (the default) the :attr:`read_preference` of this |
| 1549 | :class:`MongoClient` is used. See :mod:`~pymongo.read_preferences` |
| 1550 | for options. |
| 1551 | :param write_concern: An instance of |
| 1552 | :class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the |
| 1553 | default) the :attr:`write_concern` of this :class:`MongoClient` is |
| 1554 | used. |
| 1555 | :param read_concern: An instance of |
| 1556 | :class:`~pymongo.read_concern.ReadConcern`. If ``None`` (the |
| 1557 | default) the :attr:`read_concern` of this :class:`MongoClient` is |
| 1558 | used. |
| 1559 | |
| 1560 | .. versionchanged:: 3.5 |
| 1561 | The `name` parameter is now optional, defaulting to the database |
| 1562 | named in the MongoDB connection URI. |
| 1563 | """ |
| 1564 | if name is None: |
| 1565 | if self._default_database_name is None: |
| 1566 | raise ConfigurationError("No default database defined") |
| 1567 | name = self._default_database_name |
| 1568 | |
| 1569 | return database.Database( |
| 1570 | self, name, codec_options, read_preference, write_concern, read_concern |
| 1571 | ) |