Parse and validate a MongoDB URI. Returns a dict of the form:: { 'nodelist': , 'username': or None, 'password': or None, 'database': or None, 'collection': <co
(
uri: str,
default_port: Optional[int] = DEFAULT_PORT,
validate: bool = True,
warn: bool = False,
normalize: bool = True,
connect_timeout: Optional[float] = None,
srv_service_name: Optional[str] = None,
srv_max_hosts: Optional[int] = None,
)
| 39 | |
| 40 | |
| 41 | def parse_uri( |
| 42 | uri: str, |
| 43 | default_port: Optional[int] = DEFAULT_PORT, |
| 44 | validate: bool = True, |
| 45 | warn: bool = False, |
| 46 | normalize: bool = True, |
| 47 | connect_timeout: Optional[float] = None, |
| 48 | srv_service_name: Optional[str] = None, |
| 49 | srv_max_hosts: Optional[int] = None, |
| 50 | ) -> dict[str, Any]: |
| 51 | """Parse and validate a MongoDB URI. |
| 52 | |
| 53 | Returns a dict of the form:: |
| 54 | |
| 55 | { |
| 56 | 'nodelist': <list of (host, port) tuples>, |
| 57 | 'username': <username> or None, |
| 58 | 'password': <password> or None, |
| 59 | 'database': <database name> or None, |
| 60 | 'collection': <collection name> or None, |
| 61 | 'options': <dict of MongoDB URI options>, |
| 62 | 'fqdn': <fqdn of the MongoDB+SRV URI> or None |
| 63 | } |
| 64 | |
| 65 | If the URI scheme is "mongodb+srv://" DNS SRV and TXT lookups will be done |
| 66 | to build nodelist and options. |
| 67 | |
| 68 | :param uri: The MongoDB URI to parse. |
| 69 | :param default_port: The port number to use when one wasn't specified |
| 70 | for a host in the URI. |
| 71 | :param validate: If ``True`` (the default), validate and |
| 72 | normalize all options. Default: ``True``. |
| 73 | :param warn: When validating, if ``True`` then will warn |
| 74 | the user then ignore any invalid options or values. If ``False``, |
| 75 | validation will error when options are unsupported or values are |
| 76 | invalid. Default: ``False``. |
| 77 | :param normalize: If ``True``, convert names of URI options |
| 78 | to their internally-used names. Default: ``True``. |
| 79 | :param connect_timeout: The maximum time in milliseconds to |
| 80 | wait for a response from the DNS server. |
| 81 | :param srv_service_name: A custom SRV service name |
| 82 | |
| 83 | .. versionchanged:: 4.14 |
| 84 | ``options`` is now type ``dict`` as opposed to a ``_CaseInsensitiveDictionary``. |
| 85 | |
| 86 | .. versionchanged:: 4.6 |
| 87 | The delimiting slash (``/``) between hosts and connection options is now optional. |
| 88 | For example, "mongodb://example.com?tls=true" is now a valid URI. |
| 89 | |
| 90 | .. versionchanged:: 4.0 |
| 91 | To better follow RFC 3986, unquoted percent signs ("%") are no longer |
| 92 | supported. |
| 93 | |
| 94 | .. versionchanged:: 3.9 |
| 95 | Added the ``normalize`` parameter. |
| 96 | |
| 97 | .. versionchanged:: 3.6 |
| 98 | Added support for mongodb+srv:// URIs. |