本文整理汇总了Python中pymongo.database.Database.list_collections方法的典型用法代码示例。如果您正苦于以下问题:Python Database.list_collections方法的具体用法?Python Database.list_collections怎么用?Python Database.list_collections使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.database.Database
的用法示例。
在下文中一共展示了Database.list_collections方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_list_collections
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import list_collections [as 别名]
def test_list_collections(self):
self.client.drop_database("pymongo_test")
db = Database(self.client, "pymongo_test")
db.test.insert_one({"dummy": u"object"})
db.test.mike.insert_one({"dummy": u"object"})
results = db.list_collections()
colls = [result["name"] for result in results]
# All the collections present.
self.assertTrue("test" in colls)
self.assertTrue("test.mike" in colls)
# No collection containing a '$'.
for coll in colls:
self.assertTrue("$" not in coll)
# Duplicate check.
coll_cnt = {}
for coll in colls:
try:
# Found duplicate.
coll_cnt[coll] += 1
self.assertTrue(False)
except KeyError:
coll_cnt[coll] = 1
coll_cnt = {}
# Checking if is there any collection which don't exists.
if (len(set(colls) - set(["test","test.mike"])) == 0 or
len(set(colls) - set(["test","test.mike","system.indexes"])) == 0):
self.assertTrue(True)
else:
self.assertTrue(False)
colls = db.list_collections(filter={"name": {"$regex": "^test$"}})
self.assertEqual(1, len(list(colls)))
colls = db.list_collections(filter={"name": {"$regex": "^test.mike$"}})
self.assertEqual(1, len(list(colls)))
db.drop_collection("test")
db.create_collection("test", capped=True, size=4096)
results = db.list_collections(filter={'options.capped': True})
colls = [result["name"] for result in results]
# Checking only capped collections are present
self.assertTrue("test" in colls)
self.assertFalse("test.mike" in colls)
# No collection containing a '$'.
for coll in colls:
self.assertTrue("$" not in coll)
# Duplicate check.
coll_cnt = {}
for coll in colls:
try:
# Found duplicate.
coll_cnt[coll] += 1
self.assertTrue(False)
except KeyError:
coll_cnt[coll] = 1
coll_cnt = {}
# Checking if is there any collection which don't exists.
if (len(set(colls) - set(["test"])) == 0 or
len(set(colls) - set(["test","system.indexes"])) == 0):
self.assertTrue(True)
else:
self.assertTrue(False)
self.client.drop_database("pymongo_test")