Skip to content

Commit 9dc6d92

Browse files
committed
Don't use $query with commands PYTHON-341
1 parent 0bfefa5 commit 9dc6d92

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

pymongo/cursor.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff 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):

0 commit comments

Comments
 (0)