Skip to content

Commit da373ba

Browse files
committed
Wrapping result in union combinator.
1 parent 9b9ddcf commit da373ba

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

django_mongodb/expressions.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -122,39 +122,43 @@ def query(self, compiler, connection, lookup_name=None):
122122
if lookup_name in ("in", "range"):
123123
if subquery.aggregation_pipeline is None:
124124
subquery.aggregation_pipeline = []
125-
subquery.aggregation_pipeline.extend(
126-
[
127-
{
128-
"$facet": {
129-
"group": [
125+
wrapping_result_pipeline = [
126+
{
127+
"$facet": {
128+
"group": [
129+
{
130+
"$group": {
131+
"_id": None,
132+
"tmp_name": {
133+
"$addToSet": expr.as_mql(subquery_compiler, connection)
134+
},
135+
}
136+
}
137+
]
138+
}
139+
},
140+
{
141+
"$project": {
142+
field_name: {
143+
"$ifNull": [
130144
{
131-
"$group": {
132-
"_id": None,
133-
"tmp_name": {
134-
"$addToSet": expr.as_mql(subquery_compiler, connection)
135-
},
145+
"$getField": {
146+
"input": {"$arrayElemAt": ["$group", 0]},
147+
"field": "tmp_name",
136148
}
137-
}
149+
},
150+
[],
138151
]
139152
}
140-
},
141-
{
142-
"$project": {
143-
field_name: {
144-
"$ifNull": [
145-
{
146-
"$getField": {
147-
"input": {"$arrayElemAt": ["$group", 0]},
148-
"field": "tmp_name",
149-
}
150-
},
151-
[],
152-
]
153-
}
154-
}
155-
},
156-
]
157-
)
153+
}
154+
},
155+
]
156+
# if the subquery is a combinator, wrap the result at the end of the combinator pipeline.
157+
if subquery.query.combinator:
158+
subquery.combinator_pipeline.extend(wrapping_result_pipeline)
159+
# otherwise put at the end of subquery's pipeline.
160+
else:
161+
subquery.aggregation_pipeline.extend(wrapping_result_pipeline)
158162
# Erase project_fields since the required value is projected above.
159163
subquery.project_fields = None
160164
compiler.subqueries.append(subquery)

0 commit comments

Comments
 (0)