简体   繁体   中英

Can pymongo detect if a collection is capped?

I'm writing functionality in Python to ensure the existence, type, and size of mongodb collections. Most of these collections are capped. I know that the mongo shell includes mycollection.iscapped() , but pymongo does not seem to support this functionality.

Within the context of pymongo, what is the best way to tell if a collection is capped collection?

调用mycollection.options()返回一个带有'capped': True mycollection.options() 'capped': True的 dict 'capped': True如果它是一个有上限的集合,则为'capped': True

Found it.

# 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}

Note 'capped': 1 .

To get a list of all the capped collections in my db I used:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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