Thursday, July 4, 2024
HomeLanguagesPythonPython MongoDB – Sort

Python MongoDB – Sort

MongoDB is a cross-platform document-oriented database program and the most popular NoSQL database program. The term NoSQL means non-relational. MongoDB stores the data in the form of key-value pairs. It is an Open Source, Document Database which provides high performance and scalability along with data modeling and data management of huge sets of data in an enterprise application. MongoDB also provides the feature of Auto-Scaling. It uses JSON-like documents, which makes the database very flexible and scalable. Note: For more information, refer to MongoDB and Python

Sorting the MongoDB documents

sort() method is used for sorting the database in some order. This method accepts two parameters first is the fieldname and the second one is for the direction to sort. (By default it sorts in ascending order) 

Syntax:

sort(key_or_list, direction)

key_or_list: a single key or a list of (key, direction) pairs specifying the keys to sort on
direction (optional): only used if key_or_list is a single key, if not given ASCENDING is assumed

Note: 1 as the direction is used for ascending order and -1 as the direction is used for descending order 

Example 1: Using sort() function to sort the result alphabetically by name. Let’s suppose the database looks like this:

python-mongodb-db 

Python3




# python code to sort elements
# alphabetically in ascending order
 
import pymongo
 
 
# establishing connection
# to the database
my_client = pymongo.MongoClient('localhost', 27017)
 
# Name of the database
mydb = my_client[& quot
                  gfg & quot
                  ]
 
# Name of the collection
mynew = mydb[& quot
              names & quot
              ]
 
# sorting function
mydoc = mynew.find().sort(& quot
                           name & quot
                           )
 
for x in mydoc:
    print(x)


Output : python-mongodb-sort-1 Example 2: Sorting in descending order 

Python3




import pymongo
 
 
# establishing connection
# to the database
my_client = pymongo.MongoClient('localhost', 27017)
 
# Name of the database
mydb = my_client[& quot
                  gfg & quot
                  ]
 
# Name of the collection
mynew = mydb[& quot
              names & quot
              ]
 
# sorting function with -1
# as direction
mydoc = mynew.find().sort(& quot
                           name&quot                           , -1)
 
for x in mydoc:
    print(x)


Output : python-mongodb-sort-2

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments