0

我正在尝试使用ming ODM 映射器确保我的集合过期超过 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
        }
4

1 回答 1

1

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

于 2013-11-25T23:52:23.937 回答