Added basic querying - find and find_one
This commit is contained in:
@@ -1,5 +1,29 @@
|
||||
from connection import _get_db
|
||||
|
||||
|
||||
class QuerySet(object):
|
||||
"""A set of results returned from a query. Wraps a MongoDB cursor,
|
||||
providing Document objects as the results.
|
||||
"""
|
||||
|
||||
def __init__(self, document, cursor):
|
||||
self._document = document
|
||||
self._cursor = cursor
|
||||
|
||||
def next(self):
|
||||
"""Wrap the result in a Document object.
|
||||
"""
|
||||
return self._document._from_son(self._cursor.next())
|
||||
|
||||
def count(self):
|
||||
"""Count the selected elements in the query.
|
||||
"""
|
||||
return self._cursor.count()
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
|
||||
class CollectionManager(object):
|
||||
|
||||
def __init__(self, document):
|
||||
@@ -14,4 +38,15 @@ class CollectionManager(object):
|
||||
def _save_document(self, document):
|
||||
"""Save the provided document to the collection.
|
||||
"""
|
||||
_id = self._collection.save(document)
|
||||
_id = self._collection.save(document._to_mongo())
|
||||
document._id = _id
|
||||
|
||||
def find(self, query=None):
|
||||
"""Query the collection for document matching the provided query.
|
||||
"""
|
||||
return QuerySet(self._document, self._collection.find(query))
|
||||
|
||||
def find_one(self, query=None):
|
||||
"""Query the collection for document matching the provided query.
|
||||
"""
|
||||
return self._document._from_son(self._collection.find_one(query))
|
||||
|
||||
Reference in New Issue
Block a user