Skip to content

make querying use aggregate() instead of find() #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions django_mongodb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions django_mongodb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down