Mongodb Sorting

By Yashwant Chavan, Views 1163, Date 09-Oct-2016

Sorting mechanism is used to specify the order in which query returns the matching documents. Apply cursor.sort() method to the cursor before retrieving any documents from the database.

tags mongodb

mongoDB Sorting - cursor.sort(sort)

sort is of document type which defines the sort order for result set. { field: value } specify sort parameter field and sort order using 1 (Ascending Sorting ) and -1 (Descending Sorting). Basic syntax of sort() method is as follows

cursor.sort(sort);

For demo purpose I have used students collection and inserted some of the documents.

> db.students.insert(
[
 { roll_no: 1, first_name: "Yashwant",last_name: "Chavan",class : "X", birth_date : new Date("Dec 12, 1982")},
 { roll_no: 2, first_name: "Dinesh",last_name: "Patil",class : "X", birth_date : new Date("Nov 03, 1981")},
 { roll_no: 3, first_name: "Vinayak",last_name: "Sonar",class : "X", birth_date : new Date("Dec 26, 1980")},
 { roll_no: 4, first_name: "Kapil",last_name: "Deshpande",class : "X", birth_date : new Date("Jan 13, 1988")},
 { roll_no: 5, first_name: "Vinayak",last_name: "Bhide",class : "X", birth_date : new Date("Feb 09, 1985")}
]
);

BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 5,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

> db.students.find()
{ "_id" : ObjectId("57f9bdc2cb3c0dd7d40f35fe"), "roll_no" : 1, "first_name" : "Yashwant", "last_name" : "Chavan", "class" : "X", "birth_date" : ISODate("1982-12-11T18:30:00Z") }
{ "_id" : ObjectId("57f9bdc2cb3c0dd7d40f35ff"), "roll_no" : 2, "first_name" : "Dinesh", "last_name" : "Patil", "class" : "X", "birth_date" : ISODate("1981-11-02T18:30:00Z") }
{ "_id" : ObjectId("57f9bdc2cb3c0dd7d40f3600"), "roll_no" : 3, "first_name" : "Vinayak", "last_name" : "Sonar", "class" : "X", "birth_date" : ISODate("1980-12-25T18:30:00Z") }
{ "_id" : ObjectId("57f9bdc2cb3c0dd7d40f3601"), "roll_no" : 4, "first_name" : "Kapil", "last_name" : "Deshpande", "class" : "X", "birth_date" : ISODate("1988-01-12T18:30:00Z") }
{ "_id" : ObjectId("57f9bdc2cb3c0dd7d40f3602"), "roll_no" : 5, "first_name" : "Vinayak", "last_name" : "Bhide", "class" : "X", "birth_date" : ISODate("1985-02-08T18:30:00Z") }
>

Ascending Sorting

In following example ascending sorting is applied on first_name field ({ first_name : 1 }). To get better understanding of result set, we have displayed only first_name and birth_date fields using mongoDB projection.

> db.students.find({class: "X" }, { first_name: 1, birth_date: 1, _id: 0 }).sort( { first_name : 1 } )
{ "first_name" : "Dinesh", "birth_date" : ISODate("1981-11-02T18:30:00Z") }
{ "first_name" : "Kapil", "birth_date" : ISODate("1988-01-12T18:30:00Z") }
{ "first_name" : "Vinayak", "birth_date" : ISODate("1980-12-25T18:30:00Z") }
{ "first_name" : "Vinayak", "birth_date" : ISODate("1985-02-08T18:30:00Z") }
{ "first_name" : "Yashwant", "birth_date" : ISODate("1982-12-11T18:30:00Z") }
>

Descending Sorting

In following example, descending sorting is done on birth_date field e.g. { birth_date : -1 }

> db.students.find({class: "X" }, { first_name: 1, birth_date: 1, _id: 0 }).sort( { birth_date : -1 } )
{ "first_name" : "Kapil", "birth_date" : ISODate("1988-01-12T18:30:00Z") }
{ "first_name" : "Vinayak", "birth_date" : ISODate("1985-02-08T18:30:00Z") }
{ "first_name" : "Yashwant", "birth_date" : ISODate("1982-12-11T18:30:00Z") }
{ "first_name" : "Dinesh", "birth_date" : ISODate("1981-11-02T18:30:00Z") }
{ "first_name" : "Vinayak", "birth_date" : ISODate("1980-12-25T18:30:00Z") }
>

Sorting on multiple fields

In following example, ascending sorting is applied on first_name field and descending sorting is applied on birth_date field e.g. { first_name : 1 , birth_date : -1 }. When first_name field values are same then perform descending sort on birth_date field, Refer 3rd and 4th record.

> db.students.find({class: "X" }, { first_name: 1, birth_date: 1, _id: 0 }).sort( { first_name : 1 , birth_date : -1 } )
{ "first_name" : "Dinesh", "birth_date" : ISODate("1981-11-02T18:30:00Z") }
{ "first_name" : "Kapil", "birth_date" : ISODate("1988-01-12T18:30:00Z") }
{ "first_name" : "Vinayak", "birth_date" : ISODate("1985-02-08T18:30:00Z") }
{ "first_name" : "Vinayak", "birth_date" : ISODate("1980-12-25T18:30:00Z") }
{ "first_name" : "Yashwant", "birth_date" : ISODate("1982-12-11T18:30:00Z") }
>
Yashwant Chavan

Yashwant Chavan

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan@gmail.com