Skip to content

Commit b045925

Browse files
authored
Merge pull request #2481 from bagerard/improve_connect_doc
improve connect() doc and put more emphasis on URI string
2 parents 74ceb97 + 50d891c commit b045925

File tree

2 files changed

+61
-30
lines changed

2 files changed

+61
-30
lines changed

docs/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ Changelog
77
Development
88
===========
99
- (Fill this out as you fix issues and develop your features).
10+
- Improve connection doc #2481
1011

1112
Changes in 0.23.0
12-
===========
13+
=================
1314
- Bugfix: manually setting SequenceField in DynamicDocument doesn't increment the counter #2471
1415
- Add MongoDB 4.2 and 4.4 to CI
1516
- Add support for allowDiskUse on querysets #2468

docs/guide/connecting.rst

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Connecting to MongoDB
55
=====================
66

77
Connections in MongoEngine are registered globally and are identified with aliases.
8-
If no `alias` is provided during the connection, it will use "default" as alias.
8+
If no ``alias`` is provided during the connection, it will use "default" as alias.
99

1010
To connect to a running instance of :program:`mongod`, use the :func:`~mongoengine.connect`
1111
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::
1414
connect('project1')
1515

1616
By default, MongoEngine assumes that the :program:`mongod` instance is running
17-
on **localhost** on port **27017**. If MongoDB is running elsewhere, you should
18-
provide the :attr:`host` and :attr:`port` arguments to
19-
:func:`~mongoengine.connect`::
17+
on **localhost** on port **27017**.
2018

21-
connect('project1', host='192.168.1.35', port=12345)
19+
If MongoDB is running elsewhere, you need to provide details on how to connect. There are two ways of
20+
doing this. Using a connection string in URI format (**this is the preferred method**) or individual attributes
21+
provided as keyword arguments.
2222

23-
If the database requires authentication, :attr:`username`, :attr:`password`
24-
and :attr:`authentication_source` arguments should be provided::
23+
Connect with URI string
24+
=======================
25+
26+
When using a connection string in URI format you should specify the connection details
27+
as the :attr:`host` to :func:`~mongoengine.connect`. In a web application context for instance, the URI
28+
is typically read from the config file::
29+
30+
connect(host="mongodb://127.0.0.1:27017/my_db")
31+
32+
If the database requires authentication, you can specify it in the
33+
URI. As each database can have its own users configured, you need to tell MongoDB
34+
where to look for the user you are working with, that's what the ``?authSource=admin`` bit
35+
of the MongoDB connection string is for::
36+
37+
# Connects to 'my_db' database by authenticating
38+
# with given credentials against the 'admin' database (by default as authSource isn't provided)
39+
connect(host="mongodb://my_user:[email protected]:27017/my_db")
40+
41+
# Equivalent to previous connection but explicitly states that
42+
# it should use admin as the authentication source database
43+
connect(host="mongodb://my_user:my_password@hostname:port/my_db?authSource=admin")
44+
45+
# Connects to 'my_db' database by authenticating
46+
# with given credentials against that same database
47+
connect(host="mongodb://my_user:[email protected]:27017/my_db?authSource=my_db")
48+
49+
The URI string can also be used to configure advanced parameters like ssl, replicaSet, etc. For more
50+
information or example about URI string, you can refer to the `official doc <https://docs.mongodb.com/manual/reference/connection-string/>`_::
51+
52+
connect(host="mongodb://my_user:[email protected]:27017/my_db?authSource=admin&ssl=true&replicaSet=globaldb")
53+
54+
.. note:: URI containing SRV records (e.g "mongodb+srv://server.example.com/") can be used as well
2555

26-
connect('project1', username='webapp', password='pwd123', authentication_source='admin')
56+
Connect with keyword attributes
57+
===============================
2758

28-
URI style connections are also supported -- just supply the URI as
29-
the :attr:`host` to
30-
:func:`~mongoengine.connect`::
59+
The second option for specifying the connection details is to provide the information as keyword
60+
attributes to :func:`~mongoengine.connect`::
3161

32-
connect('project1', host='mongodb://localhost/database_name')
62+
connect('my_db', host='127.0.0.1', port=27017)
3363

34-
.. note:: URI containing SRV records (e.g mongodb+srv://server.example.com/) can be used as well as the :attr:`host`
64+
If the database requires authentication, :attr:`username`, :attr:`password`
65+
and :attr:`authentication_source` arguments should be provided::
66+
67+
connect('my_db', username='my_user', password='my_password', authentication_source='admin')
68+
69+
The set of attributes that :func:`~mongoengine.connect` recognizes includes but is not limited to:
70+
:attr:`host`, :attr:`port`, :attr:`read_preference`, :attr:`username`, :attr:`password`, :attr:`authentication_source`, :attr:`authentication_mechanism`,
71+
: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>`_
72+
can be used with :func:`~mongoengine.connect` and will simply be forwarded when instantiating the `pymongo.MongoClient`.
3573

3674
.. note:: Database, username and password from URI string overrides
37-
corresponding parameters in :func:`~mongoengine.connect`: ::
75+
corresponding parameters in :func:`~mongoengine.connect`, this should
76+
obviously be avoided: ::
3877

3978
connect(
4079
db='test',
@@ -43,28 +82,19 @@ the :attr:`host` to
4382
host='mongodb://admin:qwerty@localhost/production'
4483
)
4584

46-
will establish connection to ``production`` database using
47-
``admin`` username and ``12345`` password.
85+
will establish connection to ``production`` database using ``admin`` username and ``qwerty`` password.
4886

4987
.. note:: Calling :func:`~mongoengine.connect` without argument will establish
5088
a connection to the "test" database by default
5189

52-
Replica Sets
53-
============
54-
55-
MongoEngine supports connecting to replica sets::
56-
57-
from mongoengine import connect
58-
59-
# Regular connect
60-
connect('dbname', replicaset='rs-name')
61-
62-
# MongoDB URI-style connect
63-
connect(host='mongodb://localhost/dbname?replicaSet=rs-name')
90+
Read Preferences
91+
================
6492

65-
Read preferences are supported through the connection or via individual
93+
As stated above, Read preferences are supported through the connection but also via individual
6694
queries by passing the read_preference ::
6795

96+
from pymongo import ReadPreference
97+
6898
Bar.objects().read_preference(ReadPreference.PRIMARY)
6999
Bar.objects(read_preference=ReadPreference.PRIMARY)
70100

0 commit comments

Comments
 (0)