如何在 mongoengine 的查询集上按时间戳(降序)从 objectId 排序?

How to order by timestamp (descending order) from objectId on a queryset in mongoengine?

我有一个 collection,比如说 Cages。笼子有以下字段: _id(保存到数据库时生成),cage_type(字符串类型字段)

我正在查询。我想按 objectid 包含的时间戳降序排列。

通常我们可以通过以下方式访问时间戳: YourObject.id.generation_time

但我似乎无法在 order_by 查询中弄清楚。文档中也没有提及。

我试过:

query = Cage.objects.order_by('-_id.generation_time').first()
query = Cage.objects.order_by('-id.generation_time').first()
query = Cage.objects.order_by('-_id.getTimestamp()').first()

但是none已经解决了。

非常感谢任何帮助。感谢您的宝贵时间!

直接按ObjectId排序即可,因为它封装了创建时间戳,自然会按创建时间排序。

query = Cage.objects.order_by('-id').first()