diff --git a/django_mongodb/query.py b/django_mongodb/query.py index d018db26..84d38d28 100644 --- a/django_mongodb/query.py +++ b/django_mongodb/query.py @@ -85,15 +85,19 @@ def delete(self): def get_cursor(self): if self.query.low_mark == self.query.high_mark: return [] - fields = [col.target.column for col in self.columns] if self.columns else None - cursor = self.collection.find(self.mongo_query, fields) + fields = {col.target.column: 1 for col in self.columns} if self.columns else None + pipeline = [] + if self.mongo_query: + pipeline.append({"$match": self.mongo_query}) + if fields: + pipeline.append({"$project": fields}) if self.ordering: - cursor.sort(self.ordering) + pipeline.append({"$sort": dict(self.ordering)}) if self.query.low_mark > 0: - cursor.skip(self.query.low_mark) + pipeline.append({"$skip": self.query.low_mark}) if self.query.high_mark is not None: - cursor.limit(int(self.query.high_mark - self.query.low_mark)) - return cursor + pipeline.append({"$limit": self.query.high_mark - self.query.low_mark}) + return self.collection.aggregate(pipeline) def where_node(self, compiler, connection): diff --git a/django_mongodb/utils.py b/django_mongodb/utils.py index 552ed366..678f5979 100644 --- a/django_mongodb/utils.py +++ b/django_mongodb/utils.py @@ -83,6 +83,7 @@ def wrapper(self, *args, **kwargs): return wrapper # These are the operations that this backend uses. + aggregate = logging_wrapper("aggregate") count_documents = logging_wrapper("count_documents") insert_many = logging_wrapper("insert_many") delete_many = logging_wrapper("delete_many")