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)
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 –
Example 2: Sorting in descending order
- MongoDB and Python
- Guide to Install MongoDB with Python | Windows
- MongoDB Python | Insert and Update Data
- MongoDB python | insert(), replace_one(), replace_many()
- MongoDB python | Delete Data and Drop Collection
- Python MongoDB - Find
- Create a database in MongoDB using Python
- 3D Plotting sample Data from MongoDB Atlas Using Python
- Geospatial Queries with Python MongoDB
- Python Mongodb - Delete_one()
- Python Mongodb - Delete_many()
- Python MongoDB - Update_one()
- How to fetch data from MongoDB using Python?
- How to access a collection in MongoDB using Python?
- Python MongoDB - insert_many Query
- Drop Collection if already exists in MongoDB using Python
- Python MongoDB - Update_many Query
- Python MongoDB - insert_one Query
- Python MongoDB - find_one Query
- How to create index for MongoDB Collection using Python?
Improved By : Akanksha_Rai