Skip to content

Commit

Permalink
Added __len__ to QuerySet
Browse files Browse the repository at this point in the history
  • Loading branch information
hmarr committed Dec 24, 2009
1 parent 9bfe5c7 commit 17aef25
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
13 changes: 7 additions & 6 deletions README.rst
Expand Up @@ -19,7 +19,8 @@ Dependencies

Examples
========
::
Some simple examples of what MongoEngine code looks like::

class BlogPost(Document):
title = StringField(required=True, max_length=200)
posted = DateTimeField(default=datetime.datetime.now)
Expand Down Expand Up @@ -56,15 +57,15 @@ Examples
=== MongoEngine Docs ===
Link: hmarr.com/mongoengine

>>> BlogPost.objects.count()
>>> len(BlogPost.objects)
2
>>> HtmlPost.objects.count()
>>> len(HtmlPost.objects)
1
>>> LinkPost.objects.count()
>>> len(LinkPost.objects)
1

# Find tagged posts
>>> BlogPost.objects(tags='mongoengine').count()
>>> len(BlogPost.objects(tags='mongoengine'))
2
>>> BlogPost.objects(tags='mongodb').count()
>>> len(BlogPost.objects(tags='mongodb'))
1
3 changes: 3 additions & 0 deletions mongoengine/queryset.py
Expand Up @@ -100,6 +100,9 @@ def count(self):
"""
return self._cursor.count()

def __len__(self):
return self.count()

def limit(self, n):
"""Limit the number of returned documents to `n`. This may also be
achieved using array-slicing syntax (e.g. ``User.objects[:5]``).
Expand Down
4 changes: 2 additions & 2 deletions tests/document.py
Expand Up @@ -235,9 +235,9 @@ def test_delete(self):
"""
person = self.Person(name="Test User", age=30)
person.save()
self.assertEqual(self.Person.objects.count(), 1)
self.assertEqual(len(self.Person.objects), 1)
person.delete()
self.assertEqual(self.Person.objects.count(), 0)
self.assertEqual(len(self.Person.objects), 0)

def test_save_custom_id(self):
"""Ensure that a document may be saved with a custom _id.
Expand Down
10 changes: 5 additions & 5 deletions tests/queryset.py
Expand Up @@ -50,7 +50,7 @@ def test_find(self):

# Find all people in the collection
people = self.Person.objects
self.assertEqual(people.count(), 2)
self.assertEqual(len(people), 2)
results = list(people)
self.assertTrue(isinstance(results[0], self.Person))
self.assertTrue(isinstance(results[0].id, (pymongo.objectid.ObjectId,
Expand All @@ -62,7 +62,7 @@ def test_find(self):

# Use a query to filter the people found to just person1
people = self.Person.objects(age=20)
self.assertEqual(people.count(), 1)
self.assertEqual(len(people), 1)
person = people.next()
self.assertEqual(person.name, "User A")
self.assertEqual(person.age, 20)
Expand Down Expand Up @@ -158,13 +158,13 @@ def test_delete(self):
self.Person(name="User B", age=30).save()
self.Person(name="User C", age=40).save()

self.assertEqual(self.Person.objects.count(), 3)
self.assertEqual(len(self.Person.objects), 3)

self.Person.objects(age__lt=30).delete()
self.assertEqual(self.Person.objects.count(), 2)
self.assertEqual(len(self.Person.objects), 2)

self.Person.objects.delete()
self.assertEqual(self.Person.objects.count(), 0)
self.assertEqual(len(self.Person.objects), 0)

def test_order_by(self):
"""Ensure that QuerySets may be ordered.
Expand Down

0 comments on commit 17aef25

Please sign in to comment.