PYTHON-2766 Warn users away from cursor slices

This commit is contained in:
Bernie Hackett 2021-06-29 17:07:51 -07:00
parent 4152600ae6
commit 88480299b7
2 changed files with 13 additions and 1 deletions

View File

@ -20,7 +20,7 @@
.. describe:: c[index]
See :meth:`__getitem__`.
See :meth:`__getitem__` and read the warning.
.. automethod:: __getitem__

View File

@ -612,6 +612,18 @@ class Cursor(object):
def __getitem__(self, index):
"""Get a single document or a slice of documents from this cursor.
.. warning:: A :class:`~Cursor` is not a Python :class:`list`. Each
index access or slice requires that a new query be run using skip
and limit. Do not iterate the cursor using index accesses.
The following example is **extremely inefficient** and may return
surprising results::
cursor = db.collection.find()
# Warning: This runs a new query for each document.
# Don't do this!
for idx in range(10):
print(cursor[idx])
Raises :class:`~pymongo.errors.InvalidOperation` if this
cursor has already been used.