[英]Can pymongo detect if a collection is capped?
我正在用 Python 編寫功能以確保 mongodb 集合的存在、類型和大小。 這些集合中的大多數都有上限。 我知道 mongo shell 包括mycollection.iscapped()
,但 pymongo 似乎不支持此功能。
在 pymongo 的上下文中,判斷集合是否為上限集合的最佳方法是什么?
調用mycollection.options()
返回一個帶有'capped': True
mycollection.options()
'capped': True
的 dict 'capped': True
如果它是一個有上限的集合,則為'capped': True
。
找到了。
# Where db is a pymongo database object
>>> db.command('collstats','mycollection')
{u'count': 308291, u'ns': u'mydb.mycollection', u'ok': 1.0, u'lastExtentSize': 83890176, u'avgObjSize': 256.10971452296695, u'max': 2147483647, u'totalIndexSize': 20407296, u'flags': 0, u'capped': 1, u'numExtents': 1, u'nindexes': 1, u'storageSize': 83890176, u'indexSizes': {u'tem_1_tbm_1_ip1_1_ip2_1_p2_1': 20407296}, u'paddingFactor': 1.0, u'size': 78956320}
注意'capped': 1
。
要獲取我使用的數據庫中所有上限集合的列表:
def get_capped_collections(db):
capped_collections = []
for collcetion in db.collection_names():
options = db[collection].options()
if "capped" in options and options["capped"]:
capped_collections.append(collection)
return capped_collections
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.