Skip to content

Commit b7208d9

Browse files
committed
Erase embedded doc comparison.
1 parent 52eb282 commit b7208d9

File tree

2 files changed

+9
-83
lines changed

2 files changed

+9
-83
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 import Field
65
from django.db.models.expressions import Col
76
from django.db.models.lookups import Lookup, Transform
@@ -66,30 +65,10 @@ def get_transform(self, name):
6665
@EmbeddedModelArrayField.register_lookup
6766
class EMFArrayExact(EMFExact):
6867
def as_mql(self, compiler, connection):
69-
lhs_mql = process_lhs(self, compiler, connection)
68+
if not isinstance(self.lhs, KeyTransform):
69+
raise ValueError("error")
70+
lhs_mql, inner_lhs_mql = process_lhs(self, compiler, connection)
7071
value = process_rhs(self, compiler, connection)
71-
if isinstance(self.lhs, KeyTransform):
72-
lhs_mql, inner_lhs_mql = lhs_mql
73-
else:
74-
inner_lhs_mql = "$$item"
75-
if isinstance(value, models.Model):
76-
value, emf_data = self.model_to_dict(value)
77-
# Get conditions for any nested EmbeddedModelFields.
78-
conditions = self.get_conditions({inner_lhs_mql: (value, emf_data)})
79-
return {
80-
"$anyElementTrue": {
81-
"$ifNull": [
82-
{
83-
"$map": {
84-
"input": lhs_mql,
85-
"as": "item",
86-
"in": {"$and": conditions},
87-
}
88-
},
89-
[],
90-
]
91-
}
92-
}
9372
return {
9473
"$anyElementTrue": {
9574
"$ifNull": [
@@ -125,43 +104,22 @@ def process_rhs(self, compiler, connection):
125104
return None, [get_db_prep_value(v, connection, prepared=True) for v in values]
126105

127106
def as_mql(self, compiler, connection):
128-
lhs_mql = process_lhs(self, compiler, connection)
129-
values = process_rhs(self, compiler, connection)
130107
# Querying a subfield within the array elements (via nested KeyTransform).
131108
# Replicates MongoDB's implicit ANY-match by mapping over the array and applying
132109
# `$in` on the subfield.
133-
if isinstance(self.lhs, KeyTransform):
134-
lhs_mql, inner_lhs_mql = lhs_mql
135-
return {
136-
"$anyElementTrue": {
137-
"$ifNull": [
138-
{
139-
"$map": {
140-
"input": lhs_mql,
141-
"as": "item",
142-
"in": {"$in": [inner_lhs_mql, values]},
143-
}
144-
},
145-
[],
146-
]
147-
}
148-
}
149-
conditions = []
150-
inner_lhs_mql = "$$item"
151-
# Querying full embedded documents in the array.
152-
# Builds `$or` conditions and maps them over the array to match any full document.
153-
for value in values:
154-
value, emf_data = self.model_to_dict(value)
155-
# Get conditions for any nested EmbeddedModelFields.
156-
conditions.append({"$and": self.get_conditions({inner_lhs_mql: (value, emf_data)})})
110+
if not isinstance(self.lhs, KeyTransform):
111+
raise ValueError()
112+
lhs_mql = process_lhs(self, compiler, connection)
113+
values = process_rhs(self, compiler, connection)
114+
lhs_mql, inner_lhs_mql = lhs_mql
157115
return {
158116
"$anyElementTrue": {
159117
"$ifNull": [
160118
{
161119
"$map": {
162120
"input": lhs_mql,
163121
"as": "item",
164-
"in": {"$or": conditions},
122+
"in": {"$in": [inner_lhs_mql, values]},
165123
}
166124
},
167125
[],

tests/model_fields_/test_embedded_model.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@
1818
from django_mongodb_backend.models import EmbeddedModel
1919

2020
from .models import (
21-
A,
2221
Address,
2322
ArtifactDetail,
2423
Author,
25-
B,
2624
Book,
27-
C,
28-
D,
2925
Data,
30-
E,
3126
ExhibitSection,
3227
Holder,
3328
Library,
@@ -240,12 +235,6 @@ def test_filter_with_field(self):
240235
Movie.objects.filter(reviews__title="Horrible"), [self.clouds, self.frozen]
241236
)
242237

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-
249238
def test_filter_with_embeddedfield_path(self):
250239
self.assertCountEqual(
251240
MuseumExhibit.objects.filter(sections__0__section_number=1),
@@ -294,27 +283,6 @@ def test_overlap_simplefield(self):
294283
MuseumExhibit.objects.filter(sections__section_number__overlap=[2]), [self.wonders]
295284
)
296285

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-
318286

319287
class QueryingTests(TestCase):
320288
@classmethod

0 commit comments

Comments
 (0)