使用MongoEngine排序? [英] Sort using MongoEngine?
本文介绍了使用MongoEngine排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何像在常规mongodb查询中那样在MongoEngine中对查询对象进行排序?
How do I sort the query objects in MongoEngine, like I would in a regular mongodb query?
http://www.mongodb.org/display/DOCS/排序+和+自然+顺序
推荐答案
Mongoengine受到Django ORM的启发,与Django一样,它使用-"(以表示降序排列,即从高到低的顺序).
Mongoengine is inspired by Django's ORM, and like Django, it uses order_by
to sort the result set. order_by
takes 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).
例如:
class Person(Document):
first_name = StringField()
last_name = StringField()
age = IntField()
# later
people = Person.objects.order_by('last_name', '-age')
这篇关于使用MongoEngine排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文