Hi Programmers!, In this Python MongoDB tutorial, you will learn everything about the sort documents in MongoDB using Python in ascending or descending order. In our previous Python MongoDB tutorial, we have seen all about how to filter documents in MongoDB.
Sometimes you need to sort the MongoDB documents on a specific field, in that situation you can use the sort() method to sort queries in MongoDB.
Headings of Contents
Sort documents in mongodb using Python
PyMongo provides a sort() method to sort queries in MongoDB.The sort()
method takes two parameters that are field name and direction for sorting ( Ascending order apply by default ).
Sort mongodb query result in Ascending Order
To sort MongoDB query results in ascending order, you have to pass field name and 1 to sort() method.
let see through an example.
Example: Python MongoDB Sort
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
# database
database = client["programmingfundadb"]
# collection
collection = database["employees"]
# sort
result = collection.find().sort("age", 1)
for i in result:
print(i)
Output
{'_id': 1, 'name': 'John', 'Occupation': 'Software Enginner', 'age': 30}
{'_id': 3, 'name': 'Jarsi', 'Occupation': 'Machine Learning Enginner', 'age': 30}
{'_id': ObjectId('616a83fed28431f7a80bf993'), 'vishvajit': 'Peter', 'Occupation': 'Data Scientist', 'age': 33}
{'_id': 2, 'name': 'Peter', 'Occupation': 'DBA', 'age': 33}
{'_id': ObjectId('616a83fed28431f7a80bf994'), 'vishvajit': 'Alexa', 'Occupation': 'Machine Learning Enginner', 'age': 35}
{'_id': 4, 'name': 'Harry', 'Occupation': 'Python Dev', 'age': 35}
{'_id': ObjectId('616a83fed28431f7a80bf996'), 'vishvajit': 'Harry', 'Occupation': 'Python Developer', 'age': 36}
Note:- sort() method sorts the result by default in ascending order.
Sort MongoDB query result in Descending Order
To sort query in MongoDB in descending order, you have to pass field name and -1 to sort() method.
let see through an example.
Example: Python MongoDB Sort
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
# database
database = client["programmingfundadb"]
# collection
collection = database["students"]
# sort
result = collection.find().sort("age", -1)
for i in result:
print(i)
Output
{'_id': ObjectId('616a83fed28431f7a80bf996'), 'vishvajit': 'Harry', 'Occupation': 'Python Developer', 'age': 36}
{'_id': 4, 'name': 'Harry', 'Occupation': 'Python Dev', 'age': 35}
{'_id': ObjectId('616a83fed28431f7a80bf994'), 'vishvajit': 'Alexa', 'Occupation': 'Machine Learning Enginner', 'age': 35}
{'_id': 2, 'name': 'Peter', 'Occupation': 'DBA', 'age': 33}
{'_id': ObjectId('616a83fed28431f7a80bf993'), 'vishvajit': 'Peter', 'Occupation': 'Data Scientist', 'age': 33}
{'_id': 3, 'name': 'Jarsi', 'Occupation': 'Machine Learning Enginner', 'age': 30}
{'_id': 1, 'name': 'John', 'Occupation': 'Software Enginner', 'age': 30}
Conclusion
So in this article, you have seen all about how to sort documents in MongoDB using Python. You can sort MongoDB query results in ascending or descending order. I can apply your MongoDB documents soring on the field as your choice. I hope this article will have helped you. If you like this article, please share, support, and keep visiting for further Python MongoDB tutorials.
Thanks for reading …..
Reference:- Click Here