Skip to content

Commit c04a433

Browse files
committed
PYTHON-2130 Note that $where does not support Code with scope in MongoDB 4.4+
1 parent 5f45a69 commit c04a433

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

pymongo/cursor.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,24 +915,34 @@ def comment(self, comment):
915915
return self
916916

917917
def where(self, code):
918-
"""Adds a $where clause to this query.
918+
"""Adds a `$where`_ clause to this query.
919919
920920
The `code` argument must be an instance of :class:`basestring`
921921
(:class:`str` in python 3) or :class:`~bson.code.Code`
922922
containing a JavaScript expression. This expression will be
923923
evaluated for each document scanned. Only those documents
924924
for which the expression evaluates to *true* will be returned
925925
as results. The keyword *this* refers to the object currently
926-
being scanned.
926+
being scanned. For example::
927+
928+
# Find all documents where field "a" is less than "b" plus "c".
929+
for doc in db.test.find().where('this.a < (this.b + this.c)'):
930+
print(doc)
927931
928932
Raises :class:`TypeError` if `code` is not an instance of
929933
:class:`basestring` (:class:`str` in python 3). Raises
930934
:class:`~pymongo.errors.InvalidOperation` if this
931935
:class:`Cursor` has already been used. Only the last call to
932936
:meth:`where` applied to a :class:`Cursor` has any effect.
933937
938+
.. note:: MongoDB 4.4 drops support for :class:`~bson.code.Code`
939+
with scope variables. Consider using `$expr`_ instead.
940+
934941
:Parameters:
935942
- `code`: JavaScript expression to use as a filter
943+
944+
.. _$expr: https://docs.mongodb.com/manual/reference/operator/query/expr/
945+
.. _$where: https://docs.mongodb.com/manual/reference/operator/query/where/
936946
"""
937947
self.__check_okay_to_chain()
938948
if not isinstance(code, Code):

test/test_cursor.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,6 @@ def test_count_with_hint(self):
829829
self.assertEqual(2, collection.find().hint("x_1").count())
830830
self.assertEqual(2, collection.find().hint([("x", 1)]).count())
831831

832-
@client_context.require_version_max(4, 3, 2) # PYTHON-2130
833832
@ignore_deprecations
834833
def test_where(self):
835834
db = self.db
@@ -845,8 +844,20 @@ def test_where(self):
845844
self.assertEqual(3, len(list(db.test.find().where('this.x < 3'))))
846845
self.assertEqual(3,
847846
len(list(db.test.find().where(Code('this.x < 3')))))
848-
self.assertEqual(3, len(list(db.test.find().where(Code('this.x < i',
849-
{"i": 3})))))
847+
848+
code_with_scope = Code('this.x < i', {"i": 3})
849+
if client_context.version.at_least(4, 3, 3):
850+
# MongoDB 4.4 removed support for Code with scope.
851+
with self.assertRaises(OperationFailure):
852+
list(db.test.find().where(code_with_scope))
853+
854+
code_with_empty_scope = Code('this.x < 3', {})
855+
with self.assertRaises(OperationFailure):
856+
list(db.test.find().where(code_with_empty_scope))
857+
else:
858+
self.assertEqual(
859+
3, len(list(db.test.find().where(code_with_scope))))
860+
850861
self.assertEqual(10, len(list(db.test.find())))
851862

852863
self.assertEqual(3, db.test.find().where('this.x < 3').count())

0 commit comments

Comments
 (0)