Andy Mikhailenko avatar Andy Mikhailenko committed 15d594a

Added MongoResultSet.ids() method.

Comments (0)

Files changed (3)

 .. _pymongo: http://api.mongodb.org/python/current/
 
 """
-__version__ = '0.4.0'
+__version__ = '0.5.0'
 # remember to also update:
 #
 # * PKGBUILD
 
 
 class MongoResultSet(object):
+    """ A wrapper for pymongo cursor that wraps each item using given function
+    or class.
+
+    .. warning::
+
+       This class does not introduce caching.
+       Iterating over results exhausts the cursor.
+
+    """
     def __init__(self, cursor, wrapper):
         self._cursor = cursor
         self._wrap = wrapper
     def __getattr__(self, attr):
         return getattr(self._cursor, attr)
 
+    def ids(self):
+        """ Returns a generator with identifiers of objects in set.
+        These expressions are equivalent::
+
+            ids = (item.get_id() for item in result_set)
+
+            ids = result_set.ids()
+
+        .. warning::
+
+           This method **exhausts** the cursor, so an attempt to iterate over
+           results after calling this method will *fail*. The results are *not*
+           cached.
+
+        """
+        return (item.get_id() for item in self)
+
 #    def count(self):
 #        return self._cursor.count()
 

unittests/test_modeling.py

         entry.save(self.db)
         assert entry.get_ref() == DBRef(self.Entry.collection, entry.get_id())
 
+    def test_result_set_ids(self):
+        self.collection.insert({'title': u'Foo'})
+        self.collection.insert({'title': u'Bar'})
+        results = self.Entry.find(self.db)
+        ids_manual = [x.get_id() for x in results]
+        # new object because caching is not supported
+        ids = self.Entry.find(self.db).ids()
+        assert ids_manual == list(ids)
+
     def test_equality(self):
         """Documents are equal if all these conditions are met:
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.