Skip to content

Commit da173cf

Browse files
committed
Merge branch 'master' into fix_2484
2 parents 8fd969a + 1669f0c commit da173cf

File tree

14 files changed

+185
-99
lines changed

14 files changed

+185
-99
lines changed

.github/workflows/github-actions.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ on:
1111
tags:
1212
- 'v[0-9]+\.[0-9]+\.[0-9]+*'
1313
env:
14-
MONGODB_3_4: 3.4.19
15-
MONGODB_3_6: 3.6.13
16-
MONGODB_4_0: 4.0.13
14+
MONGODB_3_6: 3.6.14
15+
MONGODB_4_0: 4.0.23
16+
MONGODB_4_2: 4.2
17+
MONGODB_4_4: 4.4
1718

1819
PYMONGO_3_4: 3.4
1920
PYMONGO_3_6: 3.6
@@ -47,14 +48,14 @@ jobs:
4748
MONGODB: [$MONGODB_4_0]
4849
PYMONGO: [$PYMONGO_3_11]
4950
include:
50-
- python-version: 3.7
51-
MONGODB: $MONGODB_3_4
52-
PYMONGO: $PYMONGO_3_6
5351
- python-version: 3.7
5452
MONGODB: $MONGODB_3_6
5553
PYMONGO: $PYMONGO_3_9
5654
- python-version: 3.7
57-
MONGODB: $MONGODB_3_6
55+
MONGODB: $MONGODB_4_2
56+
PYMONGO: $PYMONGO_3_6
57+
- python-version: 3.7
58+
MONGODB: $MONGODB_4_4
5859
PYMONGO: $PYMONGO_3_11
5960
steps:
6061
- uses: actions/checkout@v2

.github/workflows/install_mongo.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
MONGODB=$1
44

5+
# Mongo > 4.0 follows different name convention for download links
56
mongo_build=mongodb-linux-x86_64-${MONGODB}
7+
8+
if [[ "$MONGODB" == *"4.2"* ]]; then
9+
mongo_build=mongodb-linux-x86_64-ubuntu1804-v${MONGODB}-latest
10+
elif [[ "$MONGODB" == *"4.4"* ]]; then
11+
mongo_build=mongodb-linux-x86_64-ubuntu1804-v${MONGODB}-latest
12+
fi
13+
614
wget http://fastdl.mongodb.org/linux/$mongo_build.tgz
715
tar xzf $mongo_build.tgz
8-
${PWD}/$mongo_build/bin/mongod --version
16+
17+
mongodb_dir=$(find ${PWD}/ -type d -name "mongodb-linux-x86_64*")
18+
$mongodb_dir/bin/mongod --version

.github/workflows/start_mongo.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
MONGODB=$1
44

5-
mongodb_dir=${PWD}/mongodb-linux-x86_64-${MONGODB}
5+
mongodb_dir=$(find ${PWD}/ -type d -name "mongodb-linux-x86_64*")
6+
67
mkdir $mongodb_dir/data
78
$mongodb_dir/bin/mongod --dbpath $mongodb_dir/data --logpath $mongodb_dir/mongodb.log --fork
89
mongo --eval 'db.version();' # Make sure mongo is awake

docs/changelog.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11

2+
23
=========
34
Changelog
45
=========
56

67
Development
78
===========
89
- (Fill this out as you fix issues and develop your features).
9-
- Bugfix: manually setting SequenceField in DynamicDocument doesn't increment the counter #2471
1010
- Bug fix: ignore LazyReferenceFields when clearing _changed_fields #2484
11+
- Improve connection doc #2481
12+
13+
Changes in 0.23.0
14+
=================
15+
- Bugfix: manually setting SequenceField in DynamicDocument doesn't increment the counter #2471
16+
- Add MongoDB 4.2 and 4.4 to CI
17+
- Add support for allowDiskUse on querysets #2468
1118

1219
Changes in 0.22.1
1320
=================

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

docs/guide/defining-documents.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ as the constructor's argument::
290290
content = StringField()
291291

292292

293-
.. _one-to-many-with-listfields:
293+
.. _many-to-many-with-listfields:
294294

295-
One to Many with ListFields
295+
Many to Many with ListFields
296296
'''''''''''''''''''''''''''
297297

298-
If you are implementing a one to many relationship via a list of references,
298+
If you are implementing a many to many relationship via a list of references,
299299
then the references are stored as DBRefs and to query you need to pass an
300300
instance of the object to the query::
301301

mongoengine/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
)
2929

3030

31-
VERSION = (0, 22, 1)
31+
VERSION = (0, 23, 0)
3232

3333

3434
def get_version():

mongoengine/base/fields.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ def __init__(self, field=None, **kwargs):
267267
self.field = field
268268
super().__init__(**kwargs)
269269

270+
@staticmethod
271+
def _lazy_load_refs(instance, name, ref_values, *, max_depth):
272+
_dereference = _import_class("DeReference")()
273+
documents = _dereference(
274+
ref_values,
275+
max_depth=max_depth,
276+
instance=instance,
277+
name=name,
278+
)
279+
return documents
280+
270281
def __get__(self, instance, owner):
271282
"""Descriptor to automatically dereference references."""
272283
if instance is None:
@@ -284,19 +295,15 @@ def __get__(self, instance, owner):
284295
or isinstance(self.field, (GenericReferenceField, ReferenceField))
285296
)
286297

287-
_dereference = _import_class("DeReference")()
288-
289298
if (
290299
instance._initialised
291300
and dereference
292301
and instance._data.get(self.name)
293302
and not getattr(instance._data[self.name], "_dereferenced", False)
294303
):
295-
instance._data[self.name] = _dereference(
296-
instance._data.get(self.name),
297-
max_depth=1,
298-
instance=instance,
299-
name=self.name,
304+
ref_values = instance._data.get(self.name)
305+
instance._data[self.name] = self._lazy_load_refs(
306+
ref_values=ref_values, instance=instance, name=self.name, max_depth=1
300307
)
301308
if hasattr(instance._data[self.name], "_dereferenced"):
302309
instance._data[self.name]._dereferenced = True
@@ -322,7 +329,9 @@ def __get__(self, instance, owner):
322329
and isinstance(value, (BaseList, BaseDict))
323330
and not value._dereferenced
324331
):
325-
value = _dereference(value, max_depth=1, instance=instance, name=self.name)
332+
value = self._lazy_load_refs(
333+
ref_values=value, instance=instance, name=self.name, max_depth=1
334+
)
326335
value._dereferenced = True
327336
instance._data[self.name] = value
328337

0 commit comments

Comments
 (0)