How to sort mongodb with pymongo
The pymongo is .sort(), takes key and direction as parameters.
If you need to sortby, let’s say, id then you should .sort(“_id”, 1)
For many fields:
.sort([("field1", pymongo.ASCENDING), ("field2", pymongo.DESCENDING)])
Just you can try to do this one:
db.Account.find().sort("UserName") db.Account.find().sort("UserName",pymongo.ASCENDING) db.Account.find().sort("UserName",pymongo.DESCENDING)
It’s very simple, You can do it:
db.Account.find().sort('UserName', -1) db.Account.find().sort('UserName', 1)