PYTHON-761 - Use listCollections for helper methods

This commit is contained in:
Bernie Hackett 2014-10-31 13:27:39 -07:00
parent 370e9f25c6
commit 626aeb71db
2 changed files with 27 additions and 7 deletions

View File

@ -1209,8 +1209,20 @@ class Collection(common.BaseObject):
information on the possible options. Returns an empty
dictionary if the collection has not been created yet.
"""
result = self.__database.system.namespaces.find_one(
{"name": self.__full_name})
client = self.__database.connection
result = None
if client._writable_max_wire_version() > 2:
res = self.__database.command(
"listCollections",
filter={"name": self.__name},
read_preference=ReadPreference.PRIMARY)
for doc in res.get("collections", []):
result = doc
break
else:
result = self.__database.system.namespaces.find_one(
{"name": self.__full_name})
if not result:
return {}

View File

@ -442,11 +442,19 @@ class Database(common.BaseObject):
- `include_system_collections` (optional): if ``False`` list
will not include system collections (e.g ``system.indexes``)
"""
results = self["system.namespaces"].find(
read_preference=ReadPreference.PRIMARY)
names = [r["name"] for r in results]
names = [n[len(self.__name) + 1:] for n in names
if n.startswith(self.__name + ".") and "$" not in n]
client = self.__connection
if client._writable_max_wire_version() > 2:
results = self.command("listCollections",
read_preference=ReadPreference.PRIMARY).get("collections", [])
names = [result["name"] for result in results]
else:
names = [result["name"] for result
in self["system.namespaces"].find(
read_preference=ReadPreference.PRIMARY)]
names = [n[len(self.__name) + 1:] for n in names
if n.startswith(self.__name + ".") and "$" not in n]
if not include_system_collections:
names = [n for n in names if not n.startswith("system.")]
return names