File tree Expand file tree Collapse file tree 1 file changed +13
-5
lines changed
Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -218,15 +218,23 @@ def __query_spec(self):
218218 if self .__max_scan :
219219 operators ["$maxScan" ] = self .__max_scan
220220
221- # If "query" is a top level key we must wrap the
222- # criteria in $query.
223- if operators or "query" in self .__spec :
224- spec = self .__spec
221+ if operators :
222+ # Make a shallow copy so we can cleanly rewind or clone.
223+ spec = self .__spec .copy ()
225224 if "$query" not in spec :
226225 # $query has to come first
227- spec = SON ({"$query" : self . __spec })
226+ spec = SON ({"$query" : spec })
228227 spec .update (operators )
229228 return spec
229+ # Have to wrap with $query if "query" is the first key.
230+ # We can't just use $query anytime "query" is a key as
231+ # that breaks commands like count and find_and_modify.
232+ # Checking spec.keys()[0] covers the case that the spec
233+ # was passed as an instance of SON or OrderedDict.
234+ elif ("query" in self .__spec and
235+ (len (self .__spec ) == 1 or self .__spec .keys ()[0 ] == "query" )):
236+ return SON ({"$query" : self .__spec })
237+
230238 return self .__spec
231239
232240 def __query_options (self ):
You can’t perform that action at this time.
0 commit comments