pythonmongokit

How to set TTL index for collection in MongoKit?


I want to use 'Time To Live' collection feature (http://docs.mongodb.org/manual/tutorial/expire-data/) in my app (flask+mongokit).

It is possible to create TTL index for all documents in collection via MongoKit when i describe my models or i need to use pymongo instead?

Thanks


Solution

  • You can use the pymongo layer to create a TTL index for all documents:

    http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.ensure_index

    Exemple assuming that you have a User mongokit model:

    db.User.collection.ensure_index("name", 300)
    

    note that TTL is now deprecated in pymongo 2.3. Use cache_for instead.