kramer65
kramer65

Reputation: 53843

How to sort MongoDB results with MongoEngine?

I'm just learning how to use MongoDB by using MongoEngine from Python. Simply getting all users is done as follows:

User.objects

I now want to order the users on their creation date. In the MongoDB docs I found something about .sort(), but in the MongoEngine docs I can't find anything about sort. I also tried using the following:

User.objects.sort('created')

but this leads to an AttributeError: 'BaseQuerySet' object has no attribute 'sort'.

So does anybody know how I can sort MongoDB results using MongoEngine? All tips are welcome!

Upvotes: 0

Views: 2700

Answers (1)

Daniel Watrous
Daniel Watrous

Reputation: 3821

I think you're looking for this:

Sort using MongoEngine?

The function is order_by, not sort. The hyphen sets direction.

Upvotes: 3

Related Questions