Python MongoDB Sort






<!--
main_leaderboard, all: [728,90][970,90][320,50][468,60]
-->



Python MongoDB Sort



❮ Previous
Next ❯



Sort the Result


Use the sort() method to sort the result in
ascending or descending order.


The sort() method takes one parameter for
"fieldname" and one parameter for "direction" (ascending is the default
direction).





Example


Sort the result alphabetically by name:



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)

Run example »




Sort Descending


Use the value -1 as the second parameter to sort descending.





sort("name", 1) #ascending
sort("name", -1) #descending





Example


Sort the result reverse alphabetically by name:



import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

mydoc = mycol.find().sort("name", -1)

for x in mydoc:
  print(x)

Run example »






❮ Previous
Next ❯

Popular posts from this blog

Chat program with C++ and SFML

Read files from a directory using Promises

Read an image with ADNS2610 optical sensor and Arduino Uno