@@ -25,7 +25,7 @@ def test_all(self):
2525 with self .assertNumQueries (1 ) as ctx :
2626 list (Author .objects .all ())
2727 query = ctx .captured_queries [0 ]["sql" ]
28- self .assertEqual (query , "db.queries__author.aggregate([{'$match': {'$expr': {} }}])" )
28+ self .assertEqual (query , "db.queries__author.aggregate([{'$match': {}}])" )
2929
3030 def test_join (self ):
3131 with self .assertNumQueries (1 ) as ctx :
@@ -40,12 +40,13 @@ def test_join(self):
4040 "{'$and': [{'$eq': ['$$parent__field__0', '$_id']}, "
4141 "{'$eq': ['$name', 'Bob']}]}}}], 'as': 'queries__author'}}, "
4242 "{'$unwind': '$queries__author'}, "
43- "{'$match': {'$expr': {'$eq': ['$ queries__author.name', 'Bob']} }}])" ,
43+ "{'$match': {'queries__author.name': 'Bob'}}])" ,
4444 )
4545
4646
4747class FKLookupConditionPushdownTests (TestCase ):
4848 def test_filter_on_local_and_related_fields (self ):
49+ self .maxDiff = None
4950 with self .assertNumQueries (1 ) as ctx :
5051 list (Book .objects .filter (title = "Don" , author__name = "John" ))
5152 query = ctx .captured_queries [0 ]["sql" ]
@@ -57,8 +58,8 @@ def test_filter_on_local_and_related_fields(self):
5758 "{'$match': {'$expr': {'$and': [{'$eq': ['$$parent__field__0', "
5859 "'$_id']}, {'$eq': ['$name', 'John']}]}}}], 'as': "
5960 "'queries__author'}}, {'$unwind': '$queries__author'}, {'$match': "
60- "{'$expr': {'$ and': [{'$eq': ['$ queries__author.name', 'John'] }, "
61- "{'$eq ': ['$title', ' Don']}]} }}])" ,
61+ "{'$and': [{'queries__author.name': 'John'}, "
62+ "{'title ': ' Don'}] }}])" ,
6263 )
6364
6465 def test_or_mixing_local_and_related_fields_is_not_pushable (self ):
@@ -71,9 +72,9 @@ def test_or_mixing_local_and_related_fields_is_not_pushable(self):
7172 "'queries__author', 'let': {'parent__field__0': '$author_id'}, "
7273 "'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': "
7374 "['$$parent__field__0', '$_id']}]}}}], 'as': 'queries__author'}}, "
74- "{'$unwind': '$queries__author'}, {'$match': {'$expr': {'$ or': "
75- "[{'$eq ': ['$title', ' Don'] }, {'$eq': ['$ queries__author.name', "
76- "'John']}]} }}])" ,
75+ "{'$unwind': '$queries__author'}, {'$match': {'$or': "
76+ "[{'title ': ' Don'}, {'queries__author.name': "
77+ "'John'}] }}])" ,
7778 )
7879
7980 def test_filter_on_self_join_fields (self ):
@@ -90,9 +91,9 @@ def test_filter_on_self_join_fields(self):
9091 "{'parent__field__0': '$parent_id'}, 'pipeline': [{'$match': {'$expr': "
9192 "{'$and': [{'$eq': ['$$parent__field__0', '$_id']}, {'$and': [{'$eq': "
9293 "['$group_id', ObjectId('6891ff7822e475eddc20f159')]}, {'$eq': ['$name', "
93- "'parent']}]}]}}}], 'as': 'T2'}}, {'$unwind': '$T2'}, {'$match': {'$expr': "
94- "{'$and': [{'$eq': ['$ T2.group_id', ObjectId('6891ff7822e475eddc20f159')] }, "
95- "{'$eq': ['$ T2.name', 'parent']}]} }}])" ,
94+ "'parent']}]}]}}}], 'as': 'T2'}}, {'$unwind': '$T2'}, {'$match': "
95+ "{'$and': [{'T2.group_id': ObjectId('6891ff7822e475eddc20f159')}, "
96+ "{'T2.name': 'parent'}] }}])" ,
9697 )
9798
9899 def test_filter_on_reverse_foreignkey_relation (self ):
@@ -107,12 +108,13 @@ def test_filter_on_reverse_foreignkey_relation(self):
107108 "['$$parent__field__0', '$order_id']}, {'$eq': ['$status', "
108109 "ObjectId('6891ff7822e475eddc20f159')]}]}}}], 'as': "
109110 "'queries__orderitem'}}, {'$unwind': '$queries__orderitem'}, "
110- "{'$match': {'$expr': {'$eq': ['$ queries__orderitem.status', "
111- "ObjectId('6891ff7822e475eddc20f159')]} }}, "
111+ "{'$match': {'queries__orderitem.status': "
112+ "ObjectId('6891ff7822e475eddc20f159')}}, "
112113 "{'$addFields': {'_id': '$_id'}}, {'$sort': SON([('_id', 1)])}])" ,
113114 )
114115
115116 def test_filter_on_local_and_nested_join_fields (self ):
117+ self .maxDiff = None
116118 with self .assertNumQueries (1 ) as ctx :
117119 list (
118120 Order .objects .filter (
@@ -134,11 +136,11 @@ def test_filter_on_local_and_nested_join_fields(self):
134136 "{'parent__field__0': '$queries__orderitem.order_id'}, "
135137 "'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': "
136138 "['$$parent__field__0', '$_id']}, {'$eq': ['$name', 'My Order']}]}"
137- "}}], 'as': 'T3'}}, {'$unwind': '$T3'}, {'$match': {'$expr': "
138- "{'$and': [{'$eq': ['$ T3.name', 'My Order']}, {'$eq': "
139- "['$ queries__orderitem.status', "
140- "ObjectId('6891ff7822e475eddc20f159')]}, {'$eq ': ['$name' , "
141- "'My Order']}]}}}, {'$addFields': {'_id': '$_id'}}, "
139+ "}}], 'as': 'T3'}}, {'$unwind': '$T3'}, {'$match': "
140+ "{'$and': [{'T3.name': 'My Order'}, "
141+ "{' queries__orderitem.status': ObjectId('6891ff7822e475eddc20f159')} , "
142+ "{'name ': 'My Order'}]}} , "
143+ "{'$addFields': {'_id': '$_id'}}, "
142144 "{'$sort': SON([('_id', 1)])}])" ,
143145 )
144146
@@ -157,13 +159,14 @@ def test_negated_related_filter_is_not_pushable(self):
157159 )
158160
159161 def test_or_on_local_fields_only (self ):
162+ self .maxDiff = None
160163 with self .assertNumQueries (1 ) as ctx :
161164 list (Order .objects .filter (models .Q (name = "A" ) | models .Q (name = "B" )))
162165 query = ctx .captured_queries [0 ]["sql" ]
163166 self .assertEqual (
164167 query ,
165- "db.queries__order.aggregate([{'$match': {'$expr': {'$ or': "
166- "[{'$eq ': ['$name', 'A'] }, {'$eq ': ['$name', 'B']}]} }}, "
168+ "db.queries__order.aggregate([{'$match': {'$or': "
169+ "[{'name ': 'A' }, {'name ': 'B'}] }}, "
167170 "{'$addFields': {'_id': '$_id'}}, {'$sort': SON([('_id', 1)])}])" ,
168171 )
169172
@@ -177,9 +180,8 @@ def test_or_with_mixed_pushable_and_non_pushable_fields(self):
177180 "'queries__author', 'let': {'parent__field__0': '$author_id'}, "
178181 "'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': "
179182 "['$$parent__field__0', '$_id']}]}}}], 'as': 'queries__author'}}, "
180- "{'$unwind': '$queries__author'}, {'$match': {'$expr': {'$or': "
181- "[{'$eq': ['$queries__author.name', 'John']}, {'$eq': ['$title', "
182- "'Don']}]}}}])" ,
183+ "{'$unwind': '$queries__author'}, {'$match': {'$or': "
184+ "[{'queries__author.name': 'John'}, {'title': 'Don'}]}}])" ,
183185 )
184186
185187 def test_push_equality_between_parent_and_child_fields (self ):
@@ -201,6 +203,7 @@ def test_push_equality_between_parent_and_child_fields(self):
201203
202204class M2MLookupConditionPushdownTests (TestCase ):
203205 def test_simple_related_filter_is_pushed (self ):
206+ self .maxDiff = None
204207 with self .assertNumQueries (1 ) as ctx :
205208 list (Library .objects .filter (readers__name = "Alice" ))
206209 query = ctx .captured_queries [0 ]["sql" ]
@@ -246,7 +249,7 @@ def test_simple_related_filter_is_pushed(self):
246249 ], "as": "queries__reader"
247250 }},
248251 {"$unwind": "$queries__reader"},
249- {"$match": {"$expr": {"$eq": ["$ queries__reader.name", "Alice"]} }}
252+ {"$match": {"queries__reader.name": "Alice"}}
250253 ])
251254 """
252255 self .assertEqual (query , uglify_mongo_aggregate (expected_query ))
@@ -285,12 +288,12 @@ def test_subquery_join_is_pushed(self):
285288 {"$unwind": "$U2"},
286289 {
287290 "$match": {
288- "$expr ": {
289- "$and ": [
290- {"$eq": ["$U2.name", "Alice"]},
291+ "$and ": [
292+ {"U2.name ": "Alice"},
293+ {"$expr":
291294 {"$eq": ["$library_id","$$parent__field__0"]}
292- ]
293- }
295+ }
296+ ]
294297 }
295298 },
296299 {"$project": {"a": {"$literal": 1}}},
@@ -385,12 +388,10 @@ def test_filter_on_local_and_related_fields(self):
385388 {"$unwind": "$queries__reader"},
386389 {
387390 "$match": {
388- "$expr": {
389- "$and": [
390- {"$eq": ["$name", "Central"]},
391- {"$eq": ["$queries__reader.name", "Alice"]}
392- ]
393- }
391+ "$and": [
392+ {"name": "Central"},
393+ {"queries__reader.name": "Alice"}
394+ ]
394395 }
395396 }
396397]
@@ -473,7 +474,7 @@ def test_or_on_local_fields_only(self):
473474 }
474475 },
475476 {"$unwind": "$queries__reader"},
476- {"$match": {"$expr ": {"$eq": ["$name", " Ateneo"]} }},
477+ {"$match": {"name ": " Ateneo"}},
477478 {
478479 "$project": {
479480 "queries__reader": {"foreing_field": "$queries__reader.name"},
@@ -556,12 +557,10 @@ def test_or_with_mixed_pushable_and_non_pushable_fields(self):
556557 {"$unwind": "$queries__reader"},
557558 {
558559 "$match": {
559- "$expr": {
560- "$or": [
561- {"$eq": ["$queries__reader.name", "Alice"]},
562- {"$eq": ["$name", "Central"]}
563- ]
564- }
560+ "$or": [
561+ {"queries__reader.name": "Alice"},
562+ {"name": "Central"}
563+ ]
565564 }
566565 }
567566])
0 commit comments