heynemann / motorengine

Motorengine is a port of MongoEngine for Tornado.
http://motorengine.readthedocs.org
204 stars 64 forks source link

Expiring collections #69

Closed partyzan closed 5 years ago

partyzan commented 10 years ago

Does motorengine support expiring collections as described in http://docs.mongodb.org/manual/tutorial/expire-data/ ?

Looks like mongoengine uses the following :

class MyModel(DynamicDocument):
   meta = { 
        'indexes': [{'fields': ['expiry'], 'expireAfterSeconds': 3600}]
    }
   expiry = DateTimeField()

I tried the same but it didn't work, is there a way to set ttl index through motorengine ?

heynemann commented 10 years ago

Haven't messed with expiring collections at all.. :) Should be possible to add an expiration to a field index. Right now we only support unique indexes.

partyzan commented 10 years ago

Got it, thanks for the answer.

FPurchess commented 10 years ago

@partyzan how did you implement it?

partyzan commented 8 years ago

@FPurchess we implemented it ourselves - added a common base class

class DocumentWithIndexes(Document):
    import pymongo

    ASCENDING = pymongo.ASCENDING
    DESCENDING = pymongo.DESCENDING

    def __init__(self, alias=None, **kwargs):
        indexes = self.__indexes__ if hasattr(self, "__indexes__") else []
        if len(indexes) == 0:
            return

        def ensure_index(index, **spec):
            self.objects.coll(alias).ensure_index(index, **spec)

        for index_spec in indexes:
            ensure_index(**index_spec)

        super().__init__(**kwargs)

All our documents inherit from this base class and define their indexes. For example:

class BlacklistTokenRecord(Document):    
    __collection__ = "revoked-tokens"

    __indexes__ = [
        {"index": "created_at", "expireAfterSeconds": 3600},
        {"index": "token_id", "unique": True}
    ]

    token_id = UUIDField(required=True)
    created_at = DateTimeField(required=True, default=datetime.utcnow)