Skip to content

Commit 5c5a724

Browse files
authored
Merge pull request #2790 from bagerard/idoshr-__raw__and_mongoengine
Clone [Idoshr raw and mongoengine]
2 parents cfb4265 + 04d8c0f commit 5c5a724

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

docs/changelog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Development
88
===========
99
- (Fill this out as you fix issues and develop your features).
1010
- Fix for uuidRepresentation not read when provided in URI #2741
11+
- Fix combination of __raw__ and mongoengine syntax #2773
1112
- Add tests against MongoDB 6.0 and MongoDB 7.0 in the pipeline
1213
- Fix validate() not being called when inheritance is used in EmbeddedDocument and validate is overriden #2784
1314
- Add support for readPreferenceTags in connection parameters #2644

mongoengine/queryset/transform.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,23 @@
6262
)
6363

6464

65+
def handle_raw_query(value, mongo_query):
66+
"""Combine a raw query with an existing one"""
67+
for op, v in value.items():
68+
if op not in mongo_query:
69+
mongo_query[op] = v
70+
elif op in mongo_query and isinstance(mongo_query[op], dict):
71+
mongo_query[op].update(v)
72+
73+
6574
# TODO make this less complex
6675
def query(_doc_cls=None, **kwargs):
6776
"""Transform a query from Django-style format to Mongo format."""
6877
mongo_query = {}
6978
merge_query = defaultdict(list)
7079
for key, value in sorted(kwargs.items()):
7180
if key == "__raw__":
72-
mongo_query.update(value)
81+
handle_raw_query(value, mongo_query)
7382
continue
7483

7584
parts = key.rsplit("__")
@@ -234,7 +243,7 @@ def update(_doc_cls=None, **update):
234243

235244
for key, value in update.items():
236245
if key == "__raw__":
237-
mongo_update.update(value)
246+
handle_raw_query(value, mongo_update)
238247
continue
239248

240249
parts = key.split("__")

tests/queryset/test_queryset.py

+40
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,46 @@ class BlogPost(Document):
22692269
post.reload()
22702270
assert post.slug == "When test test it"
22712271

2272+
def test_combination_of_mongoengine_and__raw__(self):
2273+
"""Ensure that the '__raw__' update/query works in combination with mongoengine syntax correctly."""
2274+
2275+
class BlogPost(Document):
2276+
slug = StringField()
2277+
foo = StringField()
2278+
tags = ListField(StringField())
2279+
2280+
BlogPost.drop_collection()
2281+
2282+
post = BlogPost(slug="test", foo="bar")
2283+
post.save()
2284+
2285+
BlogPost.objects(slug="test").update(
2286+
foo="baz",
2287+
__raw__={"$set": {"slug": "test test"}},
2288+
)
2289+
post.reload()
2290+
assert post.slug == "test test"
2291+
assert post.foo == "baz"
2292+
2293+
assert BlogPost.objects(foo="baz", __raw__={"slug": "test test"}).count() == 1
2294+
assert (
2295+
BlogPost.objects(foo__ne="bar", __raw__={"slug": {"$ne": "test"}}).count()
2296+
== 1
2297+
)
2298+
assert (
2299+
BlogPost.objects(foo="baz", __raw__={"slug": {"$ne": "test test"}}).count()
2300+
== 0
2301+
)
2302+
assert (
2303+
BlogPost.objects(foo__ne="baz", __raw__={"slug": "test test"}).count() == 0
2304+
)
2305+
assert (
2306+
BlogPost.objects(
2307+
foo__ne="baz", __raw__={"slug": {"$ne": "test test"}}
2308+
).count()
2309+
== 0
2310+
)
2311+
22722312
def test_add_to_set_each(self):
22732313
class Item(Document):
22742314
name = StringField(required=True)

0 commit comments

Comments
 (0)