Skip to content

Commit d17cac8

Browse files
author
erdenezul
authored
Merge pull request #1884 from erdenezul/limit_behavior
Clone of Limit behavior pr #1614
2 parents 8a1d280 + aa49283 commit d17cac8

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changelog
33
=========
44

5+
Development
6+
===========
7+
- QuerySet limit function behaviour: Passing 0 as parameter will return all the documents in the cursor #1611
8+
- (Fill this out as you fix issues and develop your features).
9+
=======
510
Changes in 0.15.4
611
=================
712
- Added `DateField` #513

mongoengine/queryset/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def count(self, with_limit_and_skip=False):
396396
:meth:`skip` that has been applied to this cursor into account when
397397
getting the count
398398
"""
399-
if self._limit == 0 and with_limit_and_skip or self._none:
399+
if self._limit == 0 and with_limit_and_skip is False or self._none:
400400
return 0
401401
return self._cursor.count(with_limit_and_skip=with_limit_and_skip)
402402

@@ -775,10 +775,11 @@ def limit(self, n):
775775
"""Limit the number of returned documents to `n`. This may also be
776776
achieved using array-slicing syntax (e.g. ``User.objects[:5]``).
777777
778-
:param n: the maximum number of objects to return
778+
:param n: the maximum number of objects to return if n is greater than 0.
779+
When 0 is passed, returns all the documents in the cursor
779780
"""
780781
queryset = self.clone()
781-
queryset._limit = n if n != 0 else 1
782+
queryset._limit = n
782783

783784
# If a cursor object has already been created, apply the limit to it.
784785
if queryset._cursor_obj:

tests/queryset/queryset.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ def test_limit(self):
125125
self.assertEqual(len(people2), 1)
126126
self.assertEqual(people2[0], user_a)
127127

128+
# Test limit with 0 as parameter
129+
people = self.Person.objects.limit(0)
130+
self.assertEqual(people.count(with_limit_and_skip=True), 2)
131+
self.assertEqual(len(people), 2)
132+
128133
# Test chaining of only after limit
129134
person = self.Person.objects().limit(1).only('name').first()
130135
self.assertEqual(person, user_a)
@@ -5272,6 +5277,16 @@ class BlogPost(Document):
52725277
# in a way we'd expect) should raise a TypeError, too
52735278
self.assertRaises(TypeError, BlogPost.objects(authors__in=author).count)
52745279

5280+
def test_create_count(self):
5281+
self.Person.drop_collection()
5282+
self.Person.objects.create(name="Foo")
5283+
self.Person.objects.create(name="Bar")
5284+
self.Person.objects.create(name="Baz")
5285+
self.assertEqual(self.Person.objects.count(with_limit_and_skip=True), 3)
5286+
5287+
newPerson = self.Person.objects.create(name="Foo_1")
5288+
self.assertEqual(self.Person.objects.count(with_limit_and_skip=True), 4)
5289+
52755290

52765291
if __name__ == '__main__':
52775292
unittest.main()

0 commit comments

Comments
 (0)