Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mongomock/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ def parse(self, expression):
if not isinstance(expression, dict):
return self._parse_basic_expression(expression)

if len(expression) > 1 and any(key.startswith('$') for key in expression):
raise OperationFailure(
'an expression specification must contain exactly one field, '
'the name of the expression. Found %d fields in %s'
% (len(expression), expression))

value_dict = {}
for k, v in six.iteritems(expression):
if k in arithmetic_operators:
Expand Down
7 changes: 7 additions & 0 deletions tests/test__collection_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3335,6 +3335,13 @@ def test__aggregate_project_rotate(self):
])
self.assertEqual([{'_id': 1, 'a': 2, 'b': 1, 'c': 3}], list(actual))

def test__aggregate_mixed_expression(self):
self.db.collection.insert_one({'_id': 1, 'arr': [2, 3]})
with self.assertRaises(mongomock.OperationFailure):
self.db.collection.aggregate([
{'$project': {'a': {'$literal': False, 'hint': False}}},
])

def test__find_type_array(self):
self.db.collection.insert_one({'_id': 1, 'arr': [1, 2]})
self.db.collection.insert_one({'_id': 2, 'arr': {'a': 4, 'b': 5}})
Expand Down