Skip to content

Commit 20753a0

Browse files
committed
Add workaround for the issue SERVER-114196
1 parent 2f9c8ab commit 20753a0

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

django_mongodb_backend/fields/array.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ def get_subquery_wrapping_pipeline(self, compiler, connection, field_name, expr)
320320
"tmp_name": {"$addToSet": "$tmp_name"},
321321
}
322322
},
323+
# Workaround for https://jira.mongodb.org/browse/SERVER-114196:
324+
# $$NOW becomes unavailable after $unionWith, so it must be stored
325+
# beforehand to ensure it remains accessible later in the pipeline.
326+
{"$addFields": {"__now": "$$NOW"}},
327+
# Add an empty extra document to handle default values on empty results.
323328
{"$unionWith": {"pipeline": [{"$documents": [{"tmp_name": []}]}]}},
324329
{"$limit": 1},
325330
{"$project": {field_name: "$tmp_name"}},

django_mongodb_backend/fields/embedded_model_array.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ def get_subquery_wrapping_pipeline(self, compiler, connection, field_name, expr)
167167
"tmp_name": {"$addToSet": "$tmp_name"},
168168
}
169169
},
170+
# Workaround for https://jira.mongodb.org/browse/SERVER-114196:
171+
# $$NOW becomes unavailable after $unionWith, so it must be stored
172+
# beforehand to ensure it remains accessible later in the pipeline.
173+
{"$addFields": {"__now": "$$NOW"}},
170174
# Add a dummy document in case of empty result.
171175
{"$unionWith": {"pipeline": [{"$documents": [{"tmp_name": []}]}]}},
172176
{"$limit": 1},

django_mongodb_backend/lookups.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ def get_subquery_wrapping_pipeline(self, compiler, connection, field_name, expr)
6262
"tmp_name": {"$addToSet": expr.as_mql(compiler, connection, as_expr=True)},
6363
}
6464
},
65+
# Workaround for https://jira.mongodb.org/browse/SERVER-114196:
66+
# $$NOW becomes unavailable after $unionWith, so it must be stored
67+
# beforehand to ensure it remains accessible later in the pipeline.
68+
{"$addFields": {"__now": "$$NOW"}},
69+
# Add an empty extra document to handle default values on empty results.
6570
{"$unionWith": {"pipeline": [{"$documents": [{"tmp_name": []}]}]}},
6671
{"$limit": 1},
6772
{"$project": {field_name: "$tmp_name"}},

django_mongodb_backend/query.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,16 @@ def get_pipeline(self):
9393
if self.aggregation_pipeline:
9494
pipeline.extend(self.aggregation_pipeline)
9595
if self.wrap_for_global_aggregation:
96-
# Add an empty extra document to handle default values on empty results
97-
pipeline.append({"$unionWith": {"pipeline": [{"$documents": [{}]}]}})
96+
pipeline.extend(
97+
[
98+
# Workaround for https://jira.mongodb.org/browse/SERVER-114196:
99+
# $$NOW becomes unavailable after $unionWith, so it must be stored
100+
# beforehand to ensure it remains accessible later in the pipeline.
101+
{"$addFields": {"__now": "$$NOW"}},
102+
# Add an empty extra document to handle default values on empty results.
103+
{"$unionWith": {"pipeline": [{"$documents": [{}]}]}},
104+
]
105+
)
98106
if self.project_fields:
99107
pipeline.append({"$project": self.project_fields})
100108
if self.combinator_pipeline:

tests/lookup_/tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def test_subquery_filter_constant(self):
138138
"pipeline": [
139139
{"$match": {"num": {"$gt": 2}}},
140140
{"$group": {"_id": None, "tmp_name": {"$addToSet": "$num"}}},
141+
{"$addFields": {"__now": "$$NOW"}},
141142
{"$unionWith": {"pipeline": [{"$documents": [{"tmp_name": []}]}]}},
142143
{"$limit": 1},
143144
{"$project": {"num": "$tmp_name"}},

0 commit comments

Comments
 (0)