Skip to content

Commit

Permalink
Implement basic system variables in aggregation expressions.
Browse files Browse the repository at this point in the history
Fixes #463.
  • Loading branch information
pcorpet committed Nov 2, 2018
1 parent ead77c2 commit 76b7459
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
1 change: 0 additions & 1 deletion Missing_Features.rst
Expand Up @@ -33,5 +33,4 @@ If i miss to include a feature in the below list, Please feel free to add to the
* Text search operator ($meta)
* Projection operators ($map, $let)
* Array operators ($concatArrays, $filter, $isArray, $slice)
* `Variables <https://docs.mongodb.com/manual/reference/aggregation-variables/>`_ in Aggregation Expressions
* `map_reduce <https://docs.mongodb.com/manual/reference/command/mapReduce/>`_ options (``scope`` and ``finalize``)
4 changes: 2 additions & 2 deletions mongomock/aggregate.py
Expand Up @@ -136,8 +136,8 @@ def parse(self, expression):
def _parse_basic_expression(self, expression):
if isinstance(expression, six.string_types) and expression.startswith('$'):
if expression.startswith('$$'):
raise NotImplementedError(
'Mongomock does not implement variables in aggregations yet')
get_value = helpers.embedded_item_getter(expression[2:])
return get_value({'ROOT': self._doc_dict, 'CURRENT': self._doc_dict})
get_value = helpers.embedded_item_getter(expression[1:])
return get_value(self._doc_dict)
return expression
Expand Down
15 changes: 10 additions & 5 deletions tests/test__collection_api.py
Expand Up @@ -2930,11 +2930,16 @@ def test__aggregate_system_variables(self):
{'_id': 2, 'parent_id': 1},
{'_id': 3, 'parent_id': 1},
])
with self.assertRaises(NotImplementedError):
self.db.collection.aggregate([
{'$match': {'parent_id': {'$in': [1]}}},
{'$group': {'_id': 1, 'docs': {'$push': '$$ROOT'}}},
])
actual = self.db.collection.aggregate([
{'$match': {'parent_id': {'$in': [1]}}},
{'$group': {'_id': 1, 'docs': {'$push': '$$ROOT'}}},
])
self.assertEqual(
[{'_id': 1, 'docs': [
{'_id': 2, 'parent_id': 1},
{'_id': 3, 'parent_id': 1},
]}],
list(actual))

def test__write_concern(self):
self.assertEqual({}, self.db.collection.write_concern.document)
Expand Down
12 changes: 12 additions & 0 deletions tests/test__mongomock.py
Expand Up @@ -2278,6 +2278,18 @@ def test__aggregate_subtract_dates(self):
'since': {'$subtract': ['$date', datetime.datetime(2014, 7, 4, 13, 0)]},
}}])

def test__aggregate_system_variables(self):
self.cmp.do.drop()
self.cmp.do.insert_many([
{'_id': 1},
{'_id': 2, 'parent_id': 1},
{'_id': 3, 'parent_id': 1},
])
self.cmp.compare.aggregate([
{'$match': {'parent_id': {'$in': [1]}}},
{'$group': {'_id': 1, 'docs': {'$push': '$$ROOT'}}},
])


def _LIMIT(*args):
return lambda cursor: cursor.limit(*args)
Expand Down

0 comments on commit 76b7459

Please sign in to comment.