Skip to content

Commit 42c5d13

Browse files
committed
Erase embedded doc comparison.
1 parent fb35cad commit 42c5d13

File tree

2 files changed

+9
-78
lines changed

2 files changed

+9
-78
lines changed

django_mongodb_backend/fields/embedded_model_array.py

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import difflib
22

33
from django.core.exceptions import FieldDoesNotExist
4-
from django.db import models
54
from django.db.models.expressions import Col
65
from django.db.models.lookups import Lookup, Transform
76

@@ -55,30 +54,10 @@ def get_transform(self, name):
5554
@EmbeddedModelArrayField.register_lookup
5655
class EMFArrayExact(EMFExact):
5756
def as_mql(self, compiler, connection):
58-
lhs_mql = process_lhs(self, compiler, connection)
57+
if not isinstance(self.lhs, KeyTransform):
58+
raise ValueError("error")
59+
lhs_mql, inner_lhs_mql = process_lhs(self, compiler, connection)
5960
value = process_rhs(self, compiler, connection)
60-
if isinstance(self.lhs, KeyTransform):
61-
lhs_mql, inner_lhs_mql = lhs_mql
62-
else:
63-
inner_lhs_mql = "$$item"
64-
if isinstance(value, models.Model):
65-
value, emf_data = self.model_to_dict(value)
66-
# Get conditions for any nested EmbeddedModelFields.
67-
conditions = self.get_conditions({inner_lhs_mql: (value, emf_data)})
68-
return {
69-
"$anyElementTrue": {
70-
"$ifNull": [
71-
{
72-
"$map": {
73-
"input": lhs_mql,
74-
"as": "item",
75-
"in": {"$and": conditions},
76-
}
77-
},
78-
[],
79-
]
80-
}
81-
}
8261
return {
8362
"$anyElementTrue": {
8463
"$ifNull": [
@@ -114,43 +93,22 @@ def process_rhs(self, compiler, connection):
11493
return None, [get_db_prep_value(v, connection, prepared=True) for v in values]
11594

11695
def as_mql(self, compiler, connection):
117-
lhs_mql = process_lhs(self, compiler, connection)
118-
values = process_rhs(self, compiler, connection)
11996
# Querying a subfield within the array elements (via nested KeyTransform).
12097
# Replicates MongoDB's implicit ANY-match by mapping over the array and applying
12198
# `$in` on the subfield.
122-
if isinstance(self.lhs, KeyTransform):
123-
lhs_mql, inner_lhs_mql = lhs_mql
124-
return {
125-
"$anyElementTrue": {
126-
"$ifNull": [
127-
{
128-
"$map": {
129-
"input": lhs_mql,
130-
"as": "item",
131-
"in": {"$in": [inner_lhs_mql, values]},
132-
}
133-
},
134-
[],
135-
]
136-
}
137-
}
138-
conditions = []
139-
inner_lhs_mql = "$$item"
140-
# Querying full embedded documents in the array.
141-
# Builds `$or` conditions and maps them over the array to match any full document.
142-
for value in values:
143-
value, emf_data = self.model_to_dict(value)
144-
# Get conditions for any nested EmbeddedModelFields.
145-
conditions.append({"$and": self.get_conditions({inner_lhs_mql: (value, emf_data)})})
99+
if not isinstance(self.lhs, KeyTransform):
100+
raise ValueError()
101+
lhs_mql = process_lhs(self, compiler, connection)
102+
values = process_rhs(self, compiler, connection)
103+
lhs_mql, inner_lhs_mql = lhs_mql
146104
return {
147105
"$anyElementTrue": {
148106
"$ifNull": [
149107
{
150108
"$map": {
151109
"input": lhs_mql,
152110
"as": "item",
153-
"in": {"$or": conditions},
111+
"in": {"$in": [inner_lhs_mql, values]},
154112
}
155113
},
156114
[],

tests/model_fields_/test_embedded_model.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,6 @@ def test_filter_with_field(self):
240240
Movie.objects.filter(reviews__title="Horrible"), [self.clouds, self.frozen]
241241
)
242242

243-
def test_filter_with_model(self):
244-
self.assertCountEqual(
245-
Movie.objects.filter(reviews=Review(title="Horrible", rating=2)),
246-
[self.frozen],
247-
)
248-
249243
def test_filter_with_embeddedfield_path(self):
250244
self.assertCountEqual(
251245
MuseumExhibit.objects.filter(sections__0__section_number=1),
@@ -294,27 +288,6 @@ def test_overlap_simplefield(self):
294288
MuseumExhibit.objects.filter(sections__section_number__overlap=[2]), [self.wonders]
295289
)
296290

297-
def test_overlap_emf(self):
298-
self.assertSequenceEqual(
299-
Movie.objects.filter(reviews__overlap=[Review(title="The best", rating=10)]),
300-
[self.clouds],
301-
)
302-
303-
def test_overlap_values(self):
304-
qs = Movie.objects.filter(title__in=["Clouds", "Frozen"])
305-
self.assertCountEqual(
306-
Movie.objects.filter(
307-
reviews__overlap=qs.values_list("reviews"),
308-
),
309-
[self.clouds, self.frozen],
310-
)
311-
self.assertCountEqual(
312-
Movie.objects.filter(
313-
reviews__overlap=qs.values("reviews"),
314-
),
315-
[self.clouds, self.frozen],
316-
)
317-
318291

319292
class QueryingTests(TestCase):
320293
@classmethod

0 commit comments

Comments
 (0)