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

$arrayElemAt - doesn't work when index is an expresion #607

Closed
mgodkowicz opened this issue Mar 16, 2020 · 0 comments
Closed

$arrayElemAt - doesn't work when index is an expresion #607

mgodkowicz opened this issue Mar 16, 2020 · 0 comments

Comments

@mgodkowicz
Copy link

With query:

{
    "$project": {
        "outputkey": {"$arrayElemAt": ["$input", "$idx"]}
    }
}

I get the following error:

self = <mongomock.aggregate._Parser object at 0x1357fe1c0>
operator = '$arrayElemAt', values = ['$themes', '$idx']

    def _handle_project_operator(self, operator, values):
        if operator in _GROUPING_OPERATOR_MAP:
            return _GROUPING_OPERATOR_MAP[operator](self.parse_many(values))
        if operator == '$arrayElemAt':
            key, index = values
            array = self._parse_basic_expression(key)
           return array[index]
           TypeError: list indices must be integers or slices, not str

Simple if statement that checks if index is an integer and if not parsing expression fixed the issue:

    def _handle_project_operator(self, operator, values):
        if operator in _GROUPING_OPERATOR_MAP:
            return _GROUPING_OPERATOR_MAP[operator](self.parse_many(values))
        if operator == '$arrayElemAt':
            key, index = values
            array = self._parse_basic_expression(key)
            if not isinstance(index, int):
                index = self._parse_basic_expression(index)
            return array[index]
        raise NotImplementedError("Although '%s' is a valid project operator for the "
                                  'aggregation pipeline, it is currently not implemented '
                                  'in Mongomock.' % operator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant