Skip to content

Commit 6e8c370

Browse files
authored
PYTHON-1587 Remove MongoClient.database_names and Database.collection_names (#551)
1 parent 7737679 commit 6e8c370

File tree

10 files changed

+44
-75
lines changed

10 files changed

+44
-75
lines changed

doc/api/pymongo/mongo_client.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
.. automethod:: start_session
3838
.. automethod:: list_databases
3939
.. automethod:: list_database_names
40-
.. automethod:: database_names
4140
.. automethod:: drop_database
4241
.. automethod:: get_default_database
4342
.. automethod:: get_database

doc/api/pymongo/mongo_replica_set_client.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
.. autoattribute:: codec_options
2525
.. autoattribute:: read_preference
2626
.. autoattribute:: write_concern
27-
.. automethod:: database_names
2827
.. automethod:: drop_database
2928
.. automethod:: get_database
3029
.. automethod:: close_cursor

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Breaking Changes in 4.0
2121
- Removed :meth:`pymongo.mongo_client.MongoClient.fsync`,
2222
:meth:`pymongo.mongo_client.MongoClient.unlock`, and
2323
:attr:`pymongo.mongo_client.MongoClient.is_locked`.
24+
- Removed :meth:`pymongo.mongo_client.MongoClient.database_names`.
25+
- Removed :meth:`pymongo.database.Database.collection_names`.
2426
- Removed :meth:`pymongo.collection.Collection.parallel_scan`.
2527
- Removed :mod:`pymongo.thread_util`.
2628

doc/migrate-to-pymongo4.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,37 @@ collection::
9696

9797
.. _currentOp command: https://docs.mongodb.com/manual/reference/command/currentOp/
9898

99+
MongoClient.database_names is removed
100+
.....................................
101+
102+
Removed :meth:`pymongo.mongo_client.MongoClient.database_names`. Use
103+
:meth:`~pymongo.mongo_client.MongoClient.list_database_names` instead. Code like
104+
this::
105+
106+
names = client.database_names()
107+
108+
can be changed to this::
109+
110+
names = client.list_database_names()
111+
112+
Database
113+
--------
114+
115+
Database.collection_names is removed
116+
....................................
117+
118+
Removed :meth:`pymongo.database.Database.collection_names`. Use
119+
:meth:`~pymongo.database.Database.list_collection_names` instead. Code like
120+
this::
121+
122+
names = client.collection_names()
123+
non_system_names = client.collection_names(include_system_collections=False)
124+
125+
can be changed to this::
126+
127+
names = client.list_collection_names()
128+
non_system_names = client.list_collection_names(filter={"name": {"$regex": r"^(?!system\\.)"}})
129+
99130
Removed features with no migration path
100131
---------------------------------------
101132

pymongo/database.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838

3939
_INDEX_REGEX = {"name": {"$regex": r"^(?!.*\$)"}}
40-
_SYSTEM_FILTER = {"filter": {"name": {"$regex": r"^(?!system\.)"}}}
4140

4241

4342
def _check_name(name):
@@ -863,31 +862,6 @@ def list_collection_names(self, session=None, filter=None, **kwargs):
863862
return [result["name"]
864863
for result in self.list_collections(session=session, **kwargs)]
865864

866-
def collection_names(self, include_system_collections=True,
867-
session=None):
868-
"""**DEPRECATED**: Get a list of all the collection names in this
869-
database.
870-
871-
:Parameters:
872-
- `include_system_collections` (optional): if ``False`` list
873-
will not include system collections (e.g ``system.indexes``)
874-
- `session` (optional): a
875-
:class:`~pymongo.client_session.ClientSession`.
876-
877-
.. versionchanged:: 3.7
878-
Deprecated. Use :meth:`list_collection_names` instead.
879-
880-
.. versionchanged:: 3.6
881-
Added ``session`` parameter.
882-
"""
883-
warnings.warn("collection_names is deprecated. Use "
884-
"list_collection_names instead.",
885-
DeprecationWarning, stacklevel=2)
886-
kws = {} if include_system_collections else _SYSTEM_FILTER
887-
return [result["name"]
888-
for result in self.list_collections(session=session,
889-
nameOnly=True, **kws)]
890-
891865
def drop_collection(self, name_or_collection, session=None):
892866
"""Drop a collection.
893867

pymongo/mongo_client.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,24 +1931,6 @@ def list_database_names(self, session=None):
19311931
return [doc["name"]
19321932
for doc in self.list_databases(session, nameOnly=True)]
19331933

1934-
def database_names(self, session=None):
1935-
"""**DEPRECATED**: Get a list of the names of all databases on the
1936-
connected server.
1937-
1938-
:Parameters:
1939-
- `session` (optional): a
1940-
:class:`~pymongo.client_session.ClientSession`.
1941-
1942-
.. versionchanged:: 3.7
1943-
Deprecated. Use :meth:`list_database_names` instead.
1944-
1945-
.. versionchanged:: 3.6
1946-
Added ``session`` parameter.
1947-
"""
1948-
warnings.warn("database_names is deprecated. Use list_database_names "
1949-
"instead.", DeprecationWarning, stacklevel=2)
1950-
return self.list_database_names(session)
1951-
19521934
def drop_database(self, name_or_database, session=None):
19531935
"""Drop a database.
19541936

test/test_client.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -712,23 +712,17 @@ def test_list_databases(self):
712712
for doc in cursor:
713713
self.assertEqual(["name"], list(doc))
714714

715-
def _test_list_names(self, meth):
715+
def test_list_database_names(self):
716716
self.client.pymongo_test.test.insert_one({"dummy": u"object"})
717717
self.client.pymongo_test_mike.test.insert_one({"dummy": u"object"})
718718
cmd_docs = self.client.admin.command("listDatabases")["databases"]
719719
cmd_names = [doc["name"] for doc in cmd_docs]
720720

721-
db_names = meth()
721+
db_names = self.client.list_database_names()
722722
self.assertTrue("pymongo_test" in db_names)
723723
self.assertTrue("pymongo_test_mike" in db_names)
724724
self.assertEqual(db_names, cmd_names)
725725

726-
def test_list_database_names(self):
727-
self._test_list_names(self.client.list_database_names)
728-
729-
def test_database_names(self):
730-
self._test_list_names(self.client.database_names)
731-
732726
def test_drop_database(self):
733727
self.assertRaises(TypeError, self.client.drop_database, 5)
734728
self.assertRaises(TypeError, self.client.drop_database, None)

test/test_database.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,20 @@ def test_create_collection(self):
163163
self.assertTrue(u"test.foo" in db.list_collection_names())
164164
self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
165165

166-
def _test_collection_names(self, meth, **no_system_kwargs):
166+
def test_list_collection_names(self):
167167
db = Database(self.client, "pymongo_test")
168168
db.test.insert_one({"dummy": u"object"})
169169
db.test.mike.insert_one({"dummy": u"object"})
170170

171-
colls = getattr(db, meth)()
171+
colls = db.list_collection_names()
172172
self.assertTrue("test" in colls)
173173
self.assertTrue("test.mike" in colls)
174174
for coll in colls:
175175
self.assertTrue("$" not in coll)
176176

177177
db.systemcoll.test.insert_one({})
178-
no_system_collections = getattr(db, meth)(**no_system_kwargs)
178+
no_system_collections = db.list_collection_names(
179+
filter={"name": {"$regex": r"^(?!system\.)"}})
179180
for coll in no_system_collections:
180181
self.assertTrue(not coll.startswith("system."))
181182
self.assertIn("systemcoll.test", no_system_collections)
@@ -186,19 +187,10 @@ def _test_collection_names(self, meth, **no_system_kwargs):
186187
db["coll" + str(i)].insert_one({})
187188
# No Error
188189
try:
189-
getattr(db, meth)()
190+
db.list_collection_names()
190191
finally:
191192
self.client.drop_database("many_collections")
192193

193-
def test_collection_names(self):
194-
self._test_collection_names(
195-
'collection_names', include_system_collections=False)
196-
197-
def test_list_collection_names(self):
198-
self._test_collection_names(
199-
'list_collection_names', filter={
200-
"name": {"$regex": r"^(?!system\.)"}})
201-
202194
def test_list_collection_names_filter(self):
203195
listener = OvertCommandListener()
204196
results = listener.results
@@ -306,8 +298,7 @@ def test_list_collections(self):
306298

307299
self.client.drop_database("pymongo_test")
308300

309-
def test_collection_names_single_socket(self):
310-
# Test that Database.collection_names only requires one socket.
301+
def test_list_collection_names_single_socket(self):
311302
client = rs_or_single_client(maxPoolSize=1)
312303
client.drop_database('test_collection_names_single_socket')
313304
db = client.test_collection_names_single_socket

test/test_read_preferences.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ def test_reads_from_secondary(self):
160160
self.assertEqual(10, len(list(coll.find())))
161161

162162
# Test some database helpers.
163-
self.assertIsNotNone(db.collection_names())
164163
self.assertIsNotNone(db.list_collection_names())
165164
self.assertIsNotNone(db.validate_collection("test"))
166165
self.assertIsNotNone(db.command("ping"))

test/test_session.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,11 @@ def test_end_sessions(self):
227227
client.close()
228228
self.assertEqual(len(listener.results['started']), 0)
229229

230-
@ignore_deprecations # fsync and unlock
231230
def test_client(self):
232231
client = self.client
233232
ops = [
234233
(client.server_info, [], {}),
235-
(client.database_names, [], {}),
234+
(client.list_database_names, [], {}),
236235
(client.drop_database, ['pymongo_test'], {}),
237236
]
238237

@@ -244,7 +243,6 @@ def test_database(self):
244243
ops = [
245244
(db.command, ['ping'], {}),
246245
(db.create_collection, ['collection'], {}),
247-
(db.collection_names, [], {}),
248246
(db.list_collection_names, [], {}),
249247
(db.validate_collection, ['collection'], {}),
250248
(db.drop_collection, ['collection'], {}),
@@ -1129,7 +1127,7 @@ def test_explicit_session_logout(self):
11291127
db.collection.bulk_write([InsertOne({})], session=s)
11301128

11311129
with self.assertRaisesRegex(InvalidOperation, err):
1132-
db.collection_names(session=s)
1130+
db.list_collection_names(session=s)
11331131

11341132
with self.assertRaisesRegex(InvalidOperation, err):
11351133
db.collection.find_one(session=s)
@@ -1147,7 +1145,7 @@ def test_implicit_session_logout(self):
11471145

11481146
for name, f in [
11491147
('bulk_write', lambda: db.collection.bulk_write([InsertOne({})])),
1150-
('collection_names', db.collection_names),
1148+
('list_collection_names', db.list_collection_names),
11511149
('find_one', db.collection.find_one),
11521150
('aggregate', lambda: list(db.collection.aggregate([])))
11531151
]:

0 commit comments

Comments
 (0)