Sort records in MongoDB
Sort () method in MongoDB
To sort documents in MongoDB, you need to use the sort () method . The sort () method takes a Document containing a list of fields with their sort order. To determine the sort order, 1 and -1 are used. 1 is used for ascending order, while -1 is used for descending order.
Syntax
The basic syntax of sort () method is as follows:
> db . COLLECTION_NAME . find (). sort ({ KEY : 1 })
For example
You follow Collection with the name mycol with the following data:
{ "_id" : ObjectId ( 5983548781331adf45ec5 ), "title" : "MongoDB Overview" } { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" }
The following example displays the Document sorted by title in descending order.
> db . mycol . find ({},{ "title" : 1 , _id : 0 }). sort ({ "title" :- 1 }) { "title" : "Tutorials Point Overview" } { "title" : "NoSQL Overview" } { "title" : "MongoDB Overview" } >
Remember if you don't specify the sort order, the sort () method will display the Document in ascending order.
According to Tutorialspoint
Previous post: Limit the record in MongoDB
Next lesson: Index (Index) in MongoDB
5 ★ | 1 Vote
You should read it
May be interested
- Learn about security features and authentication in MongoDBin the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
- ObjectId in MongoDByou have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.
- Text Search in MongoDBstarting with version 2.4, mongodb started supporting text indexes to search within the string content.
- How to Sort Cells Alphabetically in Excelexcel is a powerful spreadsheet tool used to store and manage text or figures. alphabetical sorting is one of excel's useful features with the ability to help you sort, access and reference data quickly. to sort cells in excel alphabetically, simply double-click by highlighting the range of cells to sort, then click the 'az sort' or 'za sort' icon in the bar. standard tool. to sort cells alphabetically in excel using the advanced sort option, highlight the entire worksheet, click the 'sort' option from the 'data' menu. , then select the column and order you want to sort in the dialog box that appears.
- Learn about Java Driver in MongoDBin the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
- Install MongoDBinstructions for installing mongodb on windows.
- Map Reduce in MongoDBin mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.
- Quick Sort (Quick Sort)quick sort is a highly efficient algorithm and is based on dividing the array into smaller pieces.
- Shell Sort in data structure and algorithmshell sort is a highly efficient sorting algorithm based on insertion sorting algorithm (insertion sort). this algorithm avoids the case of swapping positions of two distant elements in the selection algorithm (if the smaller element is in the right position quite far from the larger element on the left).
- Data modeling in MongoDBdata in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.