added map/reduce support via QuerySet.map_reduce. map_reduce operations respect query specs and ordering, but ordering is currently only applied to map/reduce collection. map/reduce may eventually require its own QuerySet to avoid slicing conflicts. results are returned as lists of MapReduceDocument objects, dynamic objects representing the query. tests and documentation included. considered in the neighborhood of 'good start'.

This commit is contained in:
blackbrrr
2010-02-09 14:56:15 -06:00
parent 3fb6307596
commit 69d3e0c4b6
4 changed files with 186 additions and 25 deletions

View File

@@ -114,3 +114,44 @@ class Document(BaseDocument):
"""
db = _get_db()
db.drop_collection(cls._meta['collection'])
class MapReduceDocument(object):
"""A document returned from a map/reduce query.
:param collection: An instance of :class:`~pymongo.Collection`
:param key: Document/result key, often an instance of
:class:`~pymongo.objectid.ObjectId`. If supplied as
an ``ObjectId`` found in the given ``collection``,
the object can be accessed via the ``key_object`` property.
:param value: The result(s) for this key. If given as a dictionary,
each key in the dictionary will be available as
an instance attribute.
.. versionadded:: 0.2.2
"""
def __init__(self, collection, key, value):
self._collection = collection
self.key = key
self.value = value
if isinstance(value, dict):
# create attributes for each named result
for k, v in value.iteritems():
setattr(self, k, v)
@property
def object(self):
"""Lazy-load the object referenced by ``self.key``. If ``self.key``
is not an ``ObjectId``, simply return ``self.key``.
"""
if not isinstance(self.key, pymongo.objectid.ObjectId):
return self.key
if not hasattr(self, "_key_object"):
self._key_object = self._collection.find_one(self.key)
return self._key_object
return self._key_object