Connect to the database specified by the 'db' argument. Connection settings may be provided here as well if the database is not running on the default port on localhost. If authentication is needed, provide username and password arguments as well. Multiple databases are supported b
(db=None, alias=DEFAULT_CONNECTION_NAME, **kwargs)
| 446 | |
| 447 | |
| 448 | def connect(db=None, alias=DEFAULT_CONNECTION_NAME, **kwargs): |
| 449 | """Connect to the database specified by the 'db' argument. |
| 450 | |
| 451 | Connection settings may be provided here as well if the database is not |
| 452 | running on the default port on localhost. If authentication is needed, |
| 453 | provide username and password arguments as well. |
| 454 | |
| 455 | Multiple databases are supported by using aliases. Provide a separate |
| 456 | `alias` to connect to a different instance of: program: `mongod`. |
| 457 | |
| 458 | In order to replace a connection identified by a given alias, you'll |
| 459 | need to call ``disconnect`` first |
| 460 | |
| 461 | See the docstring for `register_connection` for more details about all |
| 462 | supported kwargs. |
| 463 | """ |
| 464 | if alias in _connections: |
| 465 | prev_conn_setting = _connection_settings[alias] |
| 466 | new_conn_settings = _get_connection_settings(db, **kwargs) |
| 467 | |
| 468 | if new_conn_settings != prev_conn_setting: |
| 469 | err_msg = ( |
| 470 | "A different connection with alias `{}` was already " |
| 471 | "registered. Use disconnect() first" |
| 472 | ).format(alias) |
| 473 | raise ConnectionFailure(err_msg) |
| 474 | else: |
| 475 | register_connection(alias, db, **kwargs) |
| 476 | |
| 477 | return get_connection(alias) |
| 478 | |
| 479 | |
| 480 | # Support old naming convention |