MongoDB Sort 📶
Sort the Result
Use the sort() method to sort the result in ascending or descending order.
✨ Sort Result
The sort() method takes one parameter for "fieldname" and one parameter for "direction" (ascending is the default direction).
python
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydoc = mycol.find().sort("name")
for x in mydoc:
print(x)⬇️ Sort Descending
Use the value -1 as the second parameter to sort descending.
sort("name", 1)#ascendingsort("name", -1)#descending
python
mydoc = mycol.find().sort("name", -1)
for x in mydoc:
print(x)💡 Did You Know?
If you do not specify the direction, the default is ascending.