Skip to content

Commit 592a5f5

Browse files
committed
Erase embedded doc comparison.
1 parent 4c1cb36 commit 592a5f5

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
@@ -63,30 +62,10 @@ def get_transform(self, name):
6362
@EmbeddedModelArrayField.register_lookup
6463
class EMFArrayExact(EMFExact):
6564
def as_mql(self, compiler, connection):
66-
lhs_mql = process_lhs(self, compiler, connection)
65+
if not isinstance(self.lhs, KeyTransform):
66+
raise ValueError("error")
67+
lhs_mql, inner_lhs_mql = process_lhs(self, compiler, connection)
6768
value = process_rhs(self, compiler, connection)
68-
if isinstance(self.lhs, KeyTransform):
69-
lhs_mql, inner_lhs_mql = lhs_mql
70-
else:
71-
inner_lhs_mql = "$$item"
72-
if isinstance(value, models.Model):
73-
value, emf_data = self.model_to_dict(value)
74-
# Get conditions for any nested EmbeddedModelFields.
75-
conditions = self.get_conditions({inner_lhs_mql: (value, emf_data)})
76-
return {
77-
"$anyElementTrue": {
78-
"$ifNull": [
79-
{
80-
"$map": {
81-
"input": lhs_mql,
82-
"as": "item",
83-
"in": {"$and": conditions},
84-
}
85-
},
86-
[],
87-
]
88-
}
89-
}
9069
return {
9170
"$anyElementTrue": {
9271
"$ifNull": [
@@ -122,43 +101,22 @@ def process_rhs(self, compiler, connection):
122101
return None, [get_db_prep_value(v, connection, prepared=True) for v in values]
123102

124103
def as_mql(self, compiler, connection):
125-
lhs_mql = process_lhs(self, compiler, connection)
126-
values = process_rhs(self, compiler, connection)
127104
# Querying a subfield within the array elements (via nested KeyTransform).
128105
# Replicates MongoDB's implicit ANY-match by mapping over the array and applying
129106
# `$in` on the subfield.
130-
if isinstance(self.lhs, KeyTransform):
131-
lhs_mql, inner_lhs_mql = lhs_mql
132-
return {
133-
"$anyElementTrue": {
134-
"$ifNull": [
135-
{
136-
"$map": {
137-
"input": lhs_mql,
138-
"as": "item",
139-
"in": {"$in": [inner_lhs_mql, values]},
140-
}
141-
},
142-
[],
143-
]
144-
}
145-
}
146-
conditions = []
147-
inner_lhs_mql = "$$item"
148-
# Querying full embedded documents in the array.
149-
# Builds `$or` conditions and maps them over the array to match any full document.
150-
for value in values:
151-
value, emf_data = self.model_to_dict(value)
152-
# Get conditions for any nested EmbeddedModelFields.
153-
conditions.append({"$and": self.get_conditions({inner_lhs_mql: (value, emf_data)})})
107+
if not isinstance(self.lhs, KeyTransform):
108+
raise ValueError()
109+
lhs_mql = process_lhs(self, compiler, connection)
110+
values = process_rhs(self, compiler, connection)
111+
lhs_mql, inner_lhs_mql = lhs_mql
154112
return {
155113
"$anyElementTrue": {
156114
"$ifNull": [
157115
{
158116
"$map": {
159117
"input": lhs_mql,
160118
"as": "item",
161-
"in": {"$or": conditions},
119+
"in": {"$in": [inner_lhs_mql, values]},
162120
}
163121
},
164122
[],

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)