Skip to content

Commit 626aeb7

Browse files
committed
PYTHON-761 - Use listCollections for helper methods
1 parent 370e9f2 commit 626aeb7

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

pymongo/collection.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,8 +1209,20 @@ def options(self):
12091209
information on the possible options. Returns an empty
12101210
dictionary if the collection has not been created yet.
12111211
"""
1212-
result = self.__database.system.namespaces.find_one(
1213-
{"name": self.__full_name})
1212+
client = self.__database.connection
1213+
1214+
result = None
1215+
if client._writable_max_wire_version() > 2:
1216+
res = self.__database.command(
1217+
"listCollections",
1218+
filter={"name": self.__name},
1219+
read_preference=ReadPreference.PRIMARY)
1220+
for doc in res.get("collections", []):
1221+
result = doc
1222+
break
1223+
else:
1224+
result = self.__database.system.namespaces.find_one(
1225+
{"name": self.__full_name})
12141226

12151227
if not result:
12161228
return {}

pymongo/database.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,19 @@ def collection_names(self, include_system_collections=True):
442442
- `include_system_collections` (optional): if ``False`` list
443443
will not include system collections (e.g ``system.indexes``)
444444
"""
445-
results = self["system.namespaces"].find(
446-
read_preference=ReadPreference.PRIMARY)
447-
names = [r["name"] for r in results]
448-
names = [n[len(self.__name) + 1:] for n in names
449-
if n.startswith(self.__name + ".") and "$" not in n]
445+
client = self.__connection
446+
447+
if client._writable_max_wire_version() > 2:
448+
results = self.command("listCollections",
449+
read_preference=ReadPreference.PRIMARY).get("collections", [])
450+
names = [result["name"] for result in results]
451+
else:
452+
names = [result["name"] for result
453+
in self["system.namespaces"].find(
454+
read_preference=ReadPreference.PRIMARY)]
455+
names = [n[len(self.__name) + 1:] for n in names
456+
if n.startswith(self.__name + ".") and "$" not in n]
457+
450458
if not include_system_collections:
451459
names = [n for n in names if not n.startswith("system.")]
452460
return names

0 commit comments

Comments
 (0)