PYTHON-761 - Use listCollections to helper methods
This commit is contained in:
parent
b74aafb577
commit
225cd39187
@ -1286,8 +1286,21 @@ 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
|
||||
client._ensure_connected(True)
|
||||
|
||||
result = None
|
||||
if client.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}, _must_use_master=True)
|
||||
|
||||
if not result:
|
||||
return {}
|
||||
|
||||
@ -454,10 +454,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(_must_use_master=True)
|
||||
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
|
||||
client._ensure_connected(True)
|
||||
|
||||
if client.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(_must_use_master=True)]
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user