Merge pull request #2481 from bagerard/improve_connect_doc
improve connect() doc and put more emphasis on URI string
This commit is contained in:
commit
b045925efe
@ -7,9 +7,10 @@ Changelog
|
|||||||
Development
|
Development
|
||||||
===========
|
===========
|
||||||
- (Fill this out as you fix issues and develop your features).
|
- (Fill this out as you fix issues and develop your features).
|
||||||
|
- Improve connection doc #2481
|
||||||
|
|
||||||
Changes in 0.23.0
|
Changes in 0.23.0
|
||||||
===========
|
=================
|
||||||
- Bugfix: manually setting SequenceField in DynamicDocument doesn't increment the counter #2471
|
- Bugfix: manually setting SequenceField in DynamicDocument doesn't increment the counter #2471
|
||||||
- Add MongoDB 4.2 and 4.4 to CI
|
- Add MongoDB 4.2 and 4.4 to CI
|
||||||
- Add support for allowDiskUse on querysets #2468
|
- Add support for allowDiskUse on querysets #2468
|
||||||
|
@ -5,7 +5,7 @@ Connecting to MongoDB
|
|||||||
=====================
|
=====================
|
||||||
|
|
||||||
Connections in MongoEngine are registered globally and are identified with aliases.
|
Connections in MongoEngine are registered globally and are identified with aliases.
|
||||||
If no `alias` is provided during the connection, it will use "default" as alias.
|
If no ``alias`` is provided during the connection, it will use "default" as alias.
|
||||||
|
|
||||||
To connect to a running instance of :program:`mongod`, use the :func:`~mongoengine.connect`
|
To connect to a running instance of :program:`mongod`, use the :func:`~mongoengine.connect`
|
||||||
function. The first argument is the name of the database to connect to::
|
function. The first argument is the name of the database to connect to::
|
||||||
@ -14,27 +14,66 @@ function. The first argument is the name of the database to connect to::
|
|||||||
connect('project1')
|
connect('project1')
|
||||||
|
|
||||||
By default, MongoEngine assumes that the :program:`mongod` instance is running
|
By default, MongoEngine assumes that the :program:`mongod` instance is running
|
||||||
on **localhost** on port **27017**. If MongoDB is running elsewhere, you should
|
on **localhost** on port **27017**.
|
||||||
provide the :attr:`host` and :attr:`port` arguments to
|
|
||||||
:func:`~mongoengine.connect`::
|
|
||||||
|
|
||||||
connect('project1', host='192.168.1.35', port=12345)
|
If MongoDB is running elsewhere, you need to provide details on how to connect. There are two ways of
|
||||||
|
doing this. Using a connection string in URI format (**this is the preferred method**) or individual attributes
|
||||||
|
provided as keyword arguments.
|
||||||
|
|
||||||
|
Connect with URI string
|
||||||
|
=======================
|
||||||
|
|
||||||
|
When using a connection string in URI format you should specify the connection details
|
||||||
|
as the :attr:`host` to :func:`~mongoengine.connect`. In a web application context for instance, the URI
|
||||||
|
is typically read from the config file::
|
||||||
|
|
||||||
|
connect(host="mongodb://127.0.0.1:27017/my_db")
|
||||||
|
|
||||||
|
If the database requires authentication, you can specify it in the
|
||||||
|
URI. As each database can have its own users configured, you need to tell MongoDB
|
||||||
|
where to look for the user you are working with, that's what the ``?authSource=admin`` bit
|
||||||
|
of the MongoDB connection string is for::
|
||||||
|
|
||||||
|
# Connects to 'my_db' database by authenticating
|
||||||
|
# with given credentials against the 'admin' database (by default as authSource isn't provided)
|
||||||
|
connect(host="mongodb://my_user:my_password@127.0.0.1:27017/my_db")
|
||||||
|
|
||||||
|
# Equivalent to previous connection but explicitly states that
|
||||||
|
# it should use admin as the authentication source database
|
||||||
|
connect(host="mongodb://my_user:my_password@hostname:port/my_db?authSource=admin")
|
||||||
|
|
||||||
|
# Connects to 'my_db' database by authenticating
|
||||||
|
# with given credentials against that same database
|
||||||
|
connect(host="mongodb://my_user:my_password@127.0.0.1:27017/my_db?authSource=my_db")
|
||||||
|
|
||||||
|
The URI string can also be used to configure advanced parameters like ssl, replicaSet, etc. For more
|
||||||
|
information or example about URI string, you can refer to the `official doc <https://docs.mongodb.com/manual/reference/connection-string/>`_::
|
||||||
|
|
||||||
|
connect(host="mongodb://my_user:my_password@127.0.0.1:27017/my_db?authSource=admin&ssl=true&replicaSet=globaldb")
|
||||||
|
|
||||||
|
.. note:: URI containing SRV records (e.g "mongodb+srv://server.example.com/") can be used as well
|
||||||
|
|
||||||
|
Connect with keyword attributes
|
||||||
|
===============================
|
||||||
|
|
||||||
|
The second option for specifying the connection details is to provide the information as keyword
|
||||||
|
attributes to :func:`~mongoengine.connect`::
|
||||||
|
|
||||||
|
connect('my_db', host='127.0.0.1', port=27017)
|
||||||
|
|
||||||
If the database requires authentication, :attr:`username`, :attr:`password`
|
If the database requires authentication, :attr:`username`, :attr:`password`
|
||||||
and :attr:`authentication_source` arguments should be provided::
|
and :attr:`authentication_source` arguments should be provided::
|
||||||
|
|
||||||
connect('project1', username='webapp', password='pwd123', authentication_source='admin')
|
connect('my_db', username='my_user', password='my_password', authentication_source='admin')
|
||||||
|
|
||||||
URI style connections are also supported -- just supply the URI as
|
The set of attributes that :func:`~mongoengine.connect` recognizes includes but is not limited to:
|
||||||
the :attr:`host` to
|
:attr:`host`, :attr:`port`, :attr:`read_preference`, :attr:`username`, :attr:`password`, :attr:`authentication_source`, :attr:`authentication_mechanism`,
|
||||||
:func:`~mongoengine.connect`::
|
:attr:`replicaset`, :attr:`tls`, etc. Most of the parameters accepted by `pymongo.MongoClient <https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`_
|
||||||
|
can be used with :func:`~mongoengine.connect` and will simply be forwarded when instantiating the `pymongo.MongoClient`.
|
||||||
connect('project1', host='mongodb://localhost/database_name')
|
|
||||||
|
|
||||||
.. note:: URI containing SRV records (e.g mongodb+srv://server.example.com/) can be used as well as the :attr:`host`
|
|
||||||
|
|
||||||
.. note:: Database, username and password from URI string overrides
|
.. note:: Database, username and password from URI string overrides
|
||||||
corresponding parameters in :func:`~mongoengine.connect`: ::
|
corresponding parameters in :func:`~mongoengine.connect`, this should
|
||||||
|
obviously be avoided: ::
|
||||||
|
|
||||||
connect(
|
connect(
|
||||||
db='test',
|
db='test',
|
||||||
@ -43,28 +82,19 @@ the :attr:`host` to
|
|||||||
host='mongodb://admin:qwerty@localhost/production'
|
host='mongodb://admin:qwerty@localhost/production'
|
||||||
)
|
)
|
||||||
|
|
||||||
will establish connection to ``production`` database using
|
will establish connection to ``production`` database using ``admin`` username and ``qwerty`` password.
|
||||||
``admin`` username and ``12345`` password.
|
|
||||||
|
|
||||||
.. note:: Calling :func:`~mongoengine.connect` without argument will establish
|
.. note:: Calling :func:`~mongoengine.connect` without argument will establish
|
||||||
a connection to the "test" database by default
|
a connection to the "test" database by default
|
||||||
|
|
||||||
Replica Sets
|
Read Preferences
|
||||||
============
|
================
|
||||||
|
|
||||||
MongoEngine supports connecting to replica sets::
|
As stated above, Read preferences are supported through the connection but also via individual
|
||||||
|
|
||||||
from mongoengine import connect
|
|
||||||
|
|
||||||
# Regular connect
|
|
||||||
connect('dbname', replicaset='rs-name')
|
|
||||||
|
|
||||||
# MongoDB URI-style connect
|
|
||||||
connect(host='mongodb://localhost/dbname?replicaSet=rs-name')
|
|
||||||
|
|
||||||
Read preferences are supported through the connection or via individual
|
|
||||||
queries by passing the read_preference ::
|
queries by passing the read_preference ::
|
||||||
|
|
||||||
|
from pymongo import ReadPreference
|
||||||
|
|
||||||
Bar.objects().read_preference(ReadPreference.PRIMARY)
|
Bar.objects().read_preference(ReadPreference.PRIMARY)
|
||||||
Bar.objects(read_preference=ReadPreference.PRIMARY)
|
Bar.objects(read_preference=ReadPreference.PRIMARY)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user