1

I'm trying to create server side flask session extension that expires after # time. I found below Mongodb shell command in the documentation.

db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )

But how can I do it using pymodm?

Evhz
  • 8,852
  • 9
  • 51
  • 69
s1n7ax
  • 2,750
  • 6
  • 24
  • 53

2 Answers2

2

Take a look to the model definition: http://pymodm.readthedocs.io/en/stable/api/index.html?highlight=indexes#defining-models. There is a meta attribute called "indexes", that one is responsible for creating indexes. Here is an example:

import pymodm
import pymongo


class SomeModel(pymodm.MongoModel):

    ...

    class Meta:
        indexes=[pymongo.IndexModel([('field_name', <direction>)])]
Luci Furtun
  • 169
  • 1
  • 5
0

Form the docs:

indexes: This is a list of IndexModel instances that describe the indexes that should be created for this model. Indexes are created when the class definition is evaluated.

IndexModel is explained in this page.
Then add the following Meta class to your MongoModel class:

class Meta:
  indexes = [
    IndexModel([('createdAt', pymongo.ASCENDING)], expireAfterSeconds=3600)
  ]
Evhz
  • 8,852
  • 9
  • 51
  • 69