In this chapter, we will discover ways to type records in MongoDB.
The sort() Method
To kind files in MongoDB, you need to use sort() method. The method accepts a file containing a list of fields together with their sorting order. To specify sorting order 1 and -1 are used. 1 is used for ascending order even as -1 is used for descending order.
Syntax
The basic syntax of kind() technique is as follows −
>db.COLLECTION_NAME.find().sort({KEY:1})
Example
Consider the gathering myycol has the subsequent data.
{_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB Overview"}
{_id : ObjectId("507f191e810c19729de860e2"), title: "NoSQL Overview"}
{_id : ObjectId("507f191e810c19729de860e3"), title: "Tutorials Point Overview"}
Following instance will show the documents looked after with the aid of name within the descending order.
>db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})
{"title":"Tutorials Point Overview"}
{"title":"NoSQL Overview"}
{"title":"MongoDB Overview"}
>
Please be aware, if you do not specify the sorting choice, then type() technique will show the files in ascending order.