Skip to content

Commit 0767033

Browse files
committed
Merge branch 'fix_none_queryset_run_on_all_collection' of github.com:idoshr/mongoengine into idoshr-fix_none_queryset_run_on_all_collection
2 parents 042d7f2 + 28d7d65 commit 0767033

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

mongoengine/queryset/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ def create(self, **kwargs):
289289
def first(self):
290290
"""Retrieve the first object matching the query."""
291291
queryset = self.clone()
292+
if self._none or self._empty:
293+
return None
294+
292295
try:
293296
result = queryset[0]
294297
except IndexError:
@@ -551,6 +554,8 @@ def update(
551554

552555
if write_concern is None:
553556
write_concern = {}
557+
if self._none or self._empty:
558+
return 0
554559

555560
queryset = self.clone()
556561
query = queryset._query
@@ -673,6 +678,9 @@ def modify(
673678
if not update and not upsert and not remove:
674679
raise OperationError("No update parameters, must either update or remove")
675680

681+
if self._none or self._empty:
682+
return None
683+
676684
queryset = self.clone()
677685
query = queryset._query
678686
if not remove:
@@ -1306,6 +1314,10 @@ def aggregate(self, pipeline, *suppl_pipeline, **kwargs):
13061314
user_pipeline += suppl_pipeline
13071315

13081316
initial_pipeline = []
1317+
if self._none or self._empty:
1318+
initial_pipeline.append({"$limit": 1})
1319+
initial_pipeline.append({"$match": {"fldksjhkjhafds": "lasdjfhlasdhfk"}})
1320+
13091321
if self._query:
13101322
initial_pipeline.append({"$match": self._query})
13111323

tests/queryset/test_queryset.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,19 @@ class A(Document):
417417
A.drop_collection()
418418
A().save()
419419

420+
# validate collection not empty
421+
assert A.objects.count() == 1
422+
423+
# update operations
424+
assert A.objects.none().update(s="1") == 0
425+
assert A.objects.none().update_one(s="1") == 0
426+
assert A.objects.none().modify(s="1") is None
427+
428+
# validate noting change by update operations
429+
assert A.objects(s="1").count() == 0
430+
431+
# fetch queries
432+
assert A.objects.none().first() is None
420433
assert list(A.objects.none()) == []
421434
assert list(A.objects.none().all()) == []
422435
assert list(A.objects.none().limit(1)) == []

tests/queryset/test_queryset_aggregation.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,23 @@ class Aggr(Document):
276276
{"_id": agg2.id, "c": 0.0, "name": "Y"},
277277
]
278278

279+
def test_queryset_aggregation_none(self):
280+
class Person(Document):
281+
name = StringField()
282+
age = IntField()
283+
284+
Person.drop_collection()
285+
286+
p1 = Person(name="Isabella Luanna", age=16)
287+
p2 = Person(name="Wilson Junior", age=21)
288+
p3 = Person(name="Sandra Mara", age=37)
289+
Person.objects.insert([p1, p2, p3])
290+
291+
pipeline = [{"$project": {"name": {"$toUpper": "$name"}}}]
292+
data = Person.objects().none().order_by("name").aggregate(pipeline)
293+
294+
assert list(data) == []
295+
279296

280297
if __name__ == "__main__":
281298
unittest.main()

0 commit comments

Comments
 (0)