Skip to content

Commit c092620

Browse files
committed
Docstring and refactor.
1 parent b351ef1 commit c092620

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

django_mongodb/compiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ class SQLCompiler(compiler.SQLCompiler):
3131
def __init__(self, *args, **kwargs):
3232
super().__init__(*args, **kwargs)
3333
self.aggregation_pipeline = None
34+
# Dictionary mapping parent variable names to their corresponding indices.
35+
self.column_mapping = {}
3436
# A list of OrderBy objects for this query.
3537
self.order_by_objs = None
36-
# Subquery parent compiler.
37-
self.column_mapping = {}
3838
self.subqueries = []
3939

4040
def _unfold_column(self, col):

django_mongodb/expressions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ def query(self, compiler, connection, lookup_name=None):
106106
from_table = next(
107107
e.table_name for alias, e in self.alias_map.items() if self.alias_refcount[alias]
108108
)
109-
subquery.lookup_data = {
109+
# To perform a subquery, a $lookup stage is added.
110+
# This stage encapsulates the entire subquery pipeline within a $lookup operation.
111+
# The "let" clause defines the variables needed to bridge the main collection with the subquery.
112+
subquery.subquery_lookup = {
110113
"as": table_output,
111114
"from": from_table,
112115
"let": {

django_mongodb/query.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def __init__(self, compiler):
5656
self.aggregation_pipeline = compiler.aggregation_pipeline
5757
self.extra_fields = None
5858
self.combinator_pipeline = None
59-
self.lookup_data = None
59+
# Stage that encapsulates the pipeline for performing a nested subquery within a $lookup.
60+
self.subquery_lookup = None
6061

6162
def __repr__(self):
6263
return f"<MongoQuery: {self.mongo_query!r} ORDER {self.ordering!r}>"
@@ -96,10 +97,10 @@ def get_pipeline(self):
9697
pipeline.append({"$skip": self.query.low_mark})
9798
if self.query.high_mark is not None:
9899
pipeline.append({"$limit": self.query.high_mark - self.query.low_mark})
99-
if self.lookup_data:
100-
table_output = self.lookup_data["as"]
100+
if self.subquery_lookup:
101+
table_output = self.subquery_lookup["as"]
101102
pipeline = [
102-
{"$lookup": {**self.lookup_data, "pipeline": pipeline}},
103+
{"$lookup": {**self.subquery_lookup, "pipeline": pipeline}},
103104
{
104105
"$set": {
105106
table_output: {

0 commit comments

Comments
 (0)