Skip to content

Commit ab40036

Browse files
aherlihybehackett
authored andcommitted
PYTHON-1014 - Unrecognized $ operators now included in find command
1 parent b93c105 commit ab40036

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pymongo/message.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ def _gen_find_command(coll, spec, projection, skip, limit, batch_size,
171171
"""Generate a find command document."""
172172
cmd = SON([('find', coll)])
173173
if '$query' in spec:
174-
cmd.update([(_MODIFIERS[key], val)
175-
for key, val in spec.items() if key in _MODIFIERS])
174+
cmd.update([(_MODIFIERS[key], val) if key in _MODIFIERS else (key, val)
175+
for key, val in spec.items()])
176+
if '$explain' in cmd:
177+
cmd.pop('$explain')
176178
else:
177179
cmd['filter'] = spec
178180

test/test_collection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
InvalidOperation,
4848
OperationFailure,
4949
WriteConcernError)
50-
from pymongo.message import _COMMAND_OVERHEAD
50+
from pymongo.message import _COMMAND_OVERHEAD, _gen_find_command
5151
from pymongo.operations import *
5252
from pymongo.read_preferences import ReadPreference
5353
from pymongo.results import (InsertOneResult,
@@ -2144,6 +2144,12 @@ def test_find_regex(self):
21442144
for doc in c.find():
21452145
self.assertTrue(isinstance(doc['r'], Regex))
21462146

2147+
def test_find_command_generation(self):
2148+
cmd = _gen_find_command(
2149+
'coll', {'$query': {'foo': 1}, '$dumb': 2}, None, 0, 0, 0, None)
2150+
self.assertEqual(
2151+
cmd, SON([('find', 'coll'), ('$dumb', 2), ('filter', {'foo': 1})]))
2152+
21472153

21482154
if __name__ == "__main__":
21492155
unittest.main()

0 commit comments

Comments
 (0)