Skip to content

Commit 4398b4b

Browse files
committed
PYTHON-2201 Deprecate the Collection.reindex method
1 parent 463d759 commit 4398b4b

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

doc/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Version 3.11 adds support for MongoDB 4.4. Highlights include:
3838
:meth:`pymongo.collection.Collection.find`. Starting in MongoDB 4.4, the
3939
server optimizes queries against the oplog collection without requiring
4040
the user to set this flag.
41+
- Deprecated :meth:`pymongo.collection.Collection.reindex`. Use
42+
:meth:`~pymongo.database.Database.command` to run the ``reIndex`` command
43+
instead.
4144

4245
.. _validate command: https://docs.mongodb.com/manual/reference/command/validate/
4346

pymongo/collection.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,15 @@ def drop_index(self, index_or_name, session=None, **kwargs):
21782178
def reindex(self, session=None, **kwargs):
21792179
"""Rebuilds all indexes on this collection.
21802180
2181+
**DEPRECATED** - The :meth:`~reindex` method is deprecated and will be
2182+
removed in PyMongo 4.0. Use :meth:`~pymongo.database.Database.command`
2183+
to run the ``reIndex`` command directly instead::
2184+
2185+
db.command({"reIndex": "<collection_name>"})
2186+
2187+
.. note:: Starting in MongoDB 4.6, the `reIndex` command can only be
2188+
run when connected to a standalone mongod.
2189+
21812190
:Parameters:
21822191
- `session` (optional): a
21832192
:class:`~pymongo.client_session.ClientSession`.
@@ -2188,19 +2197,26 @@ def reindex(self, session=None, **kwargs):
21882197
are built in the foreground) and will be slow for large
21892198
collections.
21902199
2200+
.. versionchanged:: 3.11
2201+
Deprecated.
2202+
21912203
.. versionchanged:: 3.6
21922204
Added ``session`` parameter. Added support for arbitrary keyword
21932205
arguments.
21942206
2195-
.. versionchanged:: 3.4
2196-
Apply this collection's write concern automatically to this operation
2197-
when connected to MongoDB >= 3.4.
2198-
21992207
.. versionchanged:: 3.5
22002208
We no longer apply this collection's write concern to this operation.
22012209
MongoDB 3.4 silently ignored the write concern. MongoDB 3.6+ returns
22022210
an error if we include the write concern.
2211+
2212+
.. versionchanged:: 3.4
2213+
Apply this collection's write concern automatically to this operation
2214+
when connected to MongoDB >= 3.4.
22032215
"""
2216+
warnings.warn("The reindex method is deprecated and will be removed in "
2217+
"PyMongo 4.0. Use the Database.command method to run the "
2218+
"reIndex command instead.",
2219+
DeprecationWarning, stacklevel=2)
22042220
cmd = SON([("reIndex", self.__name)])
22052221
cmd.update(kwargs)
22062222
with self._socket_for_writes(session) as sock_info:

test/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,9 @@ def mongos_seeds(self):
671671
@property
672672
def supports_reindex(self):
673673
"""Does the connected server support reindex?"""
674-
return not (self.version.at_least(4, 1, 0) and self.is_mongos)
674+
return not ((self.version.at_least(4, 1, 0) and self.is_mongos) or
675+
(self.version.at_least(4, 5, 0) and (
676+
self.is_mongos or self.is_rs)))
675677

676678
@property
677679
def supports_getpreverror(self):

test/test_legacy_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ def test_ensure_index_deprecation(self):
105105
finally:
106106
self.db.test.drop()
107107

108+
def test_reindex_deprecation(self):
109+
self.assertRaises(DeprecationWarning, lambda: self.db.test.reindex())
110+
108111

109112
class TestLegacy(IntegrationTest):
110113

0 commit comments

Comments
 (0)