File tree 3 files changed +52
-2
lines changed
3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ Development
8
8
===========
9
9
- (Fill this out as you fix issues and develop your features).
10
10
- Fix for uuidRepresentation not read when provided in URI #2741
11
+ - Fix combination of __raw__ and mongoengine syntax #2773
11
12
- Add tests against MongoDB 6.0 and MongoDB 7.0 in the pipeline
12
13
- Fix validate() not being called when inheritance is used in EmbeddedDocument and validate is overriden #2784
13
14
- Add support for readPreferenceTags in connection parameters #2644
Original file line number Diff line number Diff line change 62
62
)
63
63
64
64
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
+
65
74
# TODO make this less complex
66
75
def query (_doc_cls = None , ** kwargs ):
67
76
"""Transform a query from Django-style format to Mongo format."""
68
77
mongo_query = {}
69
78
merge_query = defaultdict (list )
70
79
for key , value in sorted (kwargs .items ()):
71
80
if key == "__raw__" :
72
- mongo_query . update (value )
81
+ handle_raw_query (value , mongo_query )
73
82
continue
74
83
75
84
parts = key .rsplit ("__" )
@@ -234,7 +243,7 @@ def update(_doc_cls=None, **update):
234
243
235
244
for key , value in update .items ():
236
245
if key == "__raw__" :
237
- mongo_update . update (value )
246
+ handle_raw_query (value , mongo_update )
238
247
continue
239
248
240
249
parts = key .split ("__" )
Original file line number Diff line number Diff line change @@ -2269,6 +2269,46 @@ class BlogPost(Document):
2269
2269
post .reload ()
2270
2270
assert post .slug == "When test test it"
2271
2271
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
+
2272
2312
def test_add_to_set_each (self ):
2273
2313
class Item (Document ):
2274
2314
name = StringField (required = True )
You can’t perform that action at this time.
0 commit comments