mongodb 使用 MongoEngine 排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7315720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:13:57  来源:igfitidea点击:

Sort using MongoEngine?

mongodbmongoengine

提问by user235925

How do I sort the query objects in MongoEngine, like I would in a regular mongodb query?

如何在 MongoEngine 中对查询对象进行排序,就像在常规 mongodb 查询中一样?

http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order

http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order

回答by dcrosta

Mongoengine is inspired by Django's ORM, and like Django, it uses order_byto sort the result set. order_bytakes a variable number of string arguments, which are the field names (as defined in your documents) optionally preceded by a "-" (to indicate a descending sort, i.e. highest first).

Mongoengine 的灵感来自 Django 的 ORM,和 Django 一样,它用于order_by对结果集进行排序。order_by采用可变数量的字符串参数,这些参数是字段名称(如您的文档中所定义),可选地以“ -”开头(表示降序排序,即最高的在前)。

For example:

例如:

class Person(Document):
    first_name = StringField()
    last_name = StringField()
    age = IntField()

# later
people = Person.objects.order_by('last_name', '-age')