Skip to content
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

Bug? Behavior different with mongo when using $arrayElemAt with index out of range. #754

Closed
wanmeihuali opened this issue Feb 2, 2022 · 4 comments
Labels

Comments

@wanmeihuali
Copy link

From mongodb's document: https://docs.mongodb.com/manual/reference/operator/aggregation/arrayElemAt/
If idx exceeds the array bounds, $arrayElemAt does not return a result.

However, mongomock seems didn't check the out of range case. if the idx exceeds the array bounds, it throws an IndexError: list index out of range.

A simple range check here(

return array[index]
) should help.

Detail:
we have a aggregate pipeline with a stage:

            {'$lookup': {'from': 'security_info', 'localField': 'github_url',   # join the security info table with git
                         'foreignField': 'github_url', 'as': 'security_info'}},
            {'$set': {'security_info': {'$filter': {'input': '$security_info', 'as': 'security_info', 'cond': {
                                '$eq': ['$$security_info.branch', '$branch']}}}}},  # also match with branch
            {'$set': {'security_info': {'$arrayElemAt': ['$security_info', 0]}}},   # turn security field from array to object

It works on our test database on mongo compass, but it fails when using mongomock, with exception


self = <mongomock.aggregate._Parser object at 0x7f7df7ae2b00>
operator = '$arrayElemAt', values = ['$security_info', 0]

    def _handle_project_operator(self, operator, values):
        if operator in _GROUPING_OPERATOR_MAP:
            values = self.parse(values) if isinstance(values, str) else self.parse_many(values)
            return _GROUPING_OPERATOR_MAP[operator](values)
        if operator == '$arrayElemAt':
            key, index = values
            array = self._parse_basic_expression(key)
            index = self.parse(index)
>           return array[index]
E           IndexError: list index out of range

.local/lib/python3.6/site-packages/mongomock/aggregate.py:370: IndexError
@pcorpet pcorpet added the bug label Feb 3, 2022
@pcorpet
Copy link
Member

pcorpet commented Feb 3, 2022

Thanks for reporting. PRs are welcomed including regression tests to make sure the behavior between pymongo + MongoDB and mongomock stays in sync.

@pcorpet
Copy link
Member

pcorpet commented Feb 6, 2022

This will probably be fixed by #646

@pcorpet
Copy link
Member

pcorpet commented Feb 6, 2022

@wanmeihuali can you check that the fixes work for you please? If so, I'll close this issue.

@wanmeihuali
Copy link
Author

The problem is solved, Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants