繁体   English   中英

pymongo 可以检测集合是否有上限吗?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM