[SERVER-46242] $where should return an informative error message when passed a CodeWithScope argument Created: 19/Feb/20  Updated: 06/Dec/22  Resolved: 28/Feb/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Shane Harvey Assignee: Backlog - Query Team (Inactive)
Resolution: Duplicate Votes: 0
Labels: qopt-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-46492 Restore WhereMatchExpression for $where Closed
Problem/Incident
causes PYTHON-2130 Stop testing $where with CodeWScope o... Closed
Related
is related to SERVER-45454 Desugar $where to $expr + $function Closed
Assigned Teams:
Query
Operating System: ALL
Sprint: Query 2020-03-09
Participants:

 Description   

Find command $where no longer respects the scope in CodeWithScope. For example, on 4.2:

>>> db.test.insert_many([{"x": i} for i in range(10)])
<pymongo.results.InsertManyResult object at 0x109151e80>
>>> len(list(db.test.find().where('this.x < 3')))
3
>>> len(list(db.test.find().where(Code('this.x < i', {'i':3}))))
3

On v4.3.3-387-g46ee960:

>>> db.test.insert_many([{"x": i} for i in range(10)])
<pymongo.results.InsertManyResult object at 0x109151e80>
>>> len(list(db.test.find().where('this.x < 3')))
3
>>> len(list(db.test.find().where(Code('this.x < i', {'i':3}))))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/shane/git/mongo-python-driver/pymongo/cursor.py", line 1156, in next
    if len(self.__data) or self._refresh():
  File "/Users/shane/git/mongo-python-driver/pymongo/cursor.py", line 1073, in _refresh
    self.__send_message(q)
  File "/Users/shane/git/mongo-python-driver/pymongo/cursor.py", line 953, in __send_message
    response = client._run_operation_with_response(
  File "/Users/shane/git/mongo-python-driver/pymongo/mongo_client.py", line 1342, in _run_operation_with_response
    return self._retryable_read(
  File "/Users/shane/git/mongo-python-driver/pymongo/mongo_client.py", line 1464, in _retryable_read
    return func(session, server, sock_info, slave_ok)
  File "/Users/shane/git/mongo-python-driver/pymongo/mongo_client.py", line 1334, in _cmd
    return server.run_operation_with_response(
  File "/Users/shane/git/mongo-python-driver/pymongo/server.py", line 136, in run_operation_with_response
    _check_command_response(first)
  File "/Users/shane/git/mongo-python-driver/pymongo/helpers.py", line 161, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: ReferenceError: i is not defined :
@:1:15

Could this be caused by SERVER-45454 or SERVER-46098?



 Comments   
Comment by James Wahlin [ 28/Feb/20 ]

Closing as a duplicate of SERVER-46492 which will add better messaging for $where rejection of CodeWithScope..

Comment by James Wahlin [ 19/Feb/20 ]

shane.harvey I reopened this ticket and repurposed it towards improving the error message.

Comment by Shane Harvey [ 19/Feb/20 ]

Given that this is the expected behavior can we change the server to return an informative error when given a CodeWithScope? Like "CodeWithScope is no longer supported" or something?

Comment by Carl Champain (Inactive) [ 19/Feb/20 ]

I'm closing this ticket since this change was intentional (SERVER-45454).

Comment by James Wahlin [ 19/Feb/20 ]

This change is intentional and was made under SERVER-45454. CodeWithScope support has been removed for both $where and the mapReduce command for MongoDB 4.4. Additionally, CodeWithScope will be deprecated as a BSON type under SERVER-45460 for 4.4.

Generated at Thu Feb 08 05:10:55 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.