8000 support $arrayElemAt in aggregate $project by DeoLeung · Pull Request #298 · mongomock/mongomock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

support $arrayElemAt in aggregate $project #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2017
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
16 changes: 11 additions & 5 deletions mongomock/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,9 @@ def aggregate(self, pipeline, **kwargs):
'$avg',
'$sum',
'$stdDevPop',
'$stdDevSamp']
'$stdDevSamp',
'$arrayElemAt'
]
boolean_operators = ['$and', '$or', '$not'] # noqa
set_operators = [ # noqa
'$setEquals',
Expand Down Expand Up @@ -1431,6 +1433,11 @@ def _handle_project_operator(operator, values, doc_dict):
" implemented in Mongomock" % len(values))
return min(_parse_expression(values[0], doc_dict),
_parse_expression(values[1], doc_dict))
elif operator == '$arrayElemAt':
key, index = values
array = _parse_basic_expression(key, doc_dict)
v = array[index]
return v
else:
raise NotImplementedError("Although '%s' is a valid project operator for the "
"aggregation pipeline, it is currently not implemented "
Expand Down Expand Up @@ -1588,10 +1595,9 @@ def _extend_collection(out_collection, field, expression):
elif k == '$project':
filter_list = ['_id']
for field, value in iteritems(v):
if field == '_id':
if value == 0:
filter_list.remove('_id')
if value != 0:
if field == '_id' and not value:
filter_list.remove('_id')
elif value:
filter_list.append(field)
out_collection = _extend_collection(out_collection, field, value)
out_collection = [{k: v for (k, v) in x.items() if k in filter_list}
Expand Down
15 changes: 15 additions & 0 deletions tests/test__collection_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,3 +998,18 @@ def test__bulk_write_delete_many(self):
'nModified': 0, 'nUpserted': 0, 'nMatched': 0,
'writeErrors': [], 'upserted': [], 'writeConcernErrors': [],
'nRemoved': 2, 'nInserted': 0})

def test__aggregate_project_array_element_at(self):
self.db.collection.insert_one({'_id': 1, 'arr': [2, 3]})
actual = self.db.collection.aggregate([
{'$match': {'_id': 1}},
{
'$project': {
'_id': False,
'a': {
'$arrayElemAt': ['$arr', 1]
}
}
}
])
self.assertEqual([{'a': 3}], list(actual))
0