如何使用Ming设置TTL索引?

时间:2013-11-22 14:08:54

标签: ming

我正在努力确保我的收藏集使用the ming ODM mapper过期超过2周的文档:

class SloData(MappedClass):

    class __mongometa__:
        session = session
        name = 'slo_data'
        indexes = ['application']
        custom_indexes = [
            # expire records after two weeks
            dict(fields=('datetime',), expireAfterSeconds=14*24*60*60)
        ]

    _id = FieldProperty(schema.ObjectId)
    datetime = FieldProperty(schema.DateTime)
    value = FieldProperty(str)
    application = RelationProperty('SloApplication')

def ensure_indexes():
    for mapper in ming.odm.Mapper.all_mappers():
        session.ensure_indexes(mapper.collection)

但是当我删除集合并重新确保索引时,未设置expireAfterSeconds索引:

 {
                "v" : 1,
                "key" : {
                        "datetime" : 1
                },
                "ns" : "performance.slo_data",
                "name" : "datetime_1",
                "background" : true,
                "sparse" : false
        }

1 个答案:

答案 0 :(得分:1)

目前通过声明性接口声明Ming的索引只接受稀疏唯一选项。您可以使用直接公开pymongo APIsession.impl.db.collection_name.ensure_index来实现此目的。只需将其放在def ensure_indexes方法中,以便在数据库初始化时执行