Search code examples
djangosortingmongodbdjango-nonrel

Using django-mongodb, how to sort descending a capped collection?


Have this model, a class representing a capped collection at mongodb:

class Event(models.Model):
   objects = MongoDBManager()
   create_at = models.DateTimeField(auto_now_add=True)
   obj = BlobField()
   class MongoMeta:
      capped = True
      collection_size = 1000*1024*1024

What can I use to get the documents in the reverse order they are inserted?


Solution

  • This is a temporal solution at pymongo level, not django-mongodb level:

    from django.db import connections
    import pymongo
    
    conn = connections['default']
    events_col = conn.get_collection('base_event')
    events = events_col.find().sort('create_at', pymongo.DESCENDING)