Skip to content

Commit 3cb6a5c

Browse files
author
erdenezul
authored
Merge pull request #1575 from zetaben/master
Make queryset aggregates obey read_preference
2 parents 758971e + 0bc6507 commit 3cb6a5c

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

mongoengine/queryset/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,10 @@ def aggregate(self, *pipeline, **kwargs):
11981198

11991199
pipeline = initial_pipeline + list(pipeline)
12001200

1201+
if IS_PYMONGO_3 and self._read_preference is not None:
1202+
return self._collection.with_options(read_preference=self._read_preference) \
1203+
.aggregate(pipeline, cursor={}, **kwargs)
1204+
12011205
return self._collection.aggregate(pipeline, cursor={}, **kwargs)
12021206

12031207
# JS functionality

tests/queryset/queryset.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4438,6 +4438,26 @@ class Bar(Document):
44384438
self.assertEqual(bars._cursor._Cursor__read_preference,
44394439
ReadPreference.SECONDARY_PREFERRED)
44404440

4441+
@needs_mongodb_v26
4442+
def test_read_preference_aggregation_framework(self):
4443+
class Bar(Document):
4444+
txt = StringField()
4445+
4446+
meta = {
4447+
'indexes': ['txt']
4448+
}
4449+
# Aggregates with read_preference
4450+
bars = Bar.objects \
4451+
.read_preference(ReadPreference.SECONDARY_PREFERRED) \
4452+
.aggregate()
4453+
if IS_PYMONGO_3:
4454+
self.assertEqual(bars._CommandCursor__collection.read_preference,
4455+
ReadPreference.SECONDARY_PREFERRED)
4456+
else:
4457+
self.assertNotEqual(bars._CommandCursor__collection.read_preference,
4458+
ReadPreference.SECONDARY_PREFERRED)
4459+
4460+
44414461
def test_json_simple(self):
44424462

44434463
class Embedded(EmbeddedDocument):

0 commit comments

Comments
 (0)