w3resource

MongoDB: cursor.sort() method

cursor.sort

The cursor.sort() method is used to return results ordered according to a sort specification. Specify in the sort parameter the field or fields to sort by and a value of 1 or -1 to specify an ascending or descending sort respectively.

Syntax:

cursor.sort(sort)

Parameter:

Name Description Required /
Optional
Type
sort A document that defines the sort order of the result set. Required document

A collection prod_master contain the following documents.


{ "_id" : 5, "item" : { "name" : "mango", "type" : "cortland" }, "cost" : 1.29 }
{ "_id" : 9, "item" : { "name" : "mango", "type" : "fuji" }, "cost" : 1.99 }
{ "_id" : 7, "item" : { "name" : "apple", "type" : "honey crisp" }, "cost" : 1.99 }
{ "_id" : 10, "item" : { "name" : "mango", "type" : "jonagold" }, "cost" : 1.29 }
{ "_id" : 1, "item" : { "name" : "banana", "type" : "jonathan" }, "cost" : 1.29 }
{ "_id" : 6, "item" : { "name" : "apple", "type" : "mcintosh" }, "cost" : 1.29 }
{ "_id" : 8, "item" : { "name" : "orange", "type" : "cara cara" }, "cost" : 2.99 }
{ "_id" : 4, "item" : { "name" : "apple", "type" : "navel" }, "cost" : 1.39 }
{ "_id" : 3, "item" : { "name" : "orange", "type" : "satsuma" }, "cost" : 1.99 }
{ "_id" : 2, "item" : { "name" : "banana", "type" : "valencia" }, "cost" : 0.99 }

Example: MongoDB: cursor.sort() method

The following query, which returns all documents from the prod_master collection, does not specify a sort order:

db.prod_master.find();

The query returns the documents in indeterminate prod_master:


"_id" : 5, "item" : { "name" : "mango", "type" : "cortland" }, "cost" : 1.29 }
"_id" : 9, "item" : { "name" : "mango", "type" : "fuji" }, "cost" : 1.99 }
"_id" : 7, "item" : { "name" : "apple", "type" : "honey crisp" }, "cost" : 1.99 }
"_id" : 10, "item" : { "name" : "mango", "type" : "jonagold" }, "cost" : 1.29 }
"_id" : 1, "item" : { "name" : "banana", "type" : "jonathan" }, "cost" : 1.29 }
"_id" : 6, "item" : { "name" : "apple", "type" : "mcintosh" }, "cost" : 1.29 }
"_id" : 8, "item" : { "name" : "orange", "type" : "cara cara" }, "cost" : 2.99 }
"_id" : 4, "item" : { "name" : "apple", "type" : "navel" }, "cost" : 1.39 }
"_id" : 3, "item" : { "name" : "orange", "type" : "satsuma" }, "cost" : 1.99 }
"_id" : 2, "item" : { "name" : "banana", "type" : "valencia" }, "cost" : 0.99 }

The following query specifies a sort on the cost field in descending order.

db.prod_master.find().sort( { cost: -1 } )

The query returns the following documents, in descending order of cost:


> db.prod_master.find().sort( { cost: -1 } )
{ "_id" : 8, "item" : { "name" : "orange", "type" : "cara cara" }, "cost" : 2.99 }
{ "_id" : 9, "item" : { "name" : "mango", "type" : "fuji" }, "cost" : 1.99 }
{ "_id" : 7, "item" : { "name" : "apple", "type" : "honey crisp" }, "cost" : 1.99 }
{ "_id" : 3, "item" : { "name" : "orange", "type" : "satsuma" }, "cost" : 1.99 }
{ "_id" : 4, "item" : { "name" : "apple", "type" : "navel" }, "cost" : 1.39 }
{ "_id" : 5, "item" : { "name" : "mango", "type" : "cortland" }, "cost" : 1.29 }
{ "_id" : 10, "item" : { "name" : "mango", "type" : "jonagold" }, "cost" : 1.29 }
{ "_id" : 1, "item" : { "name" : "banana", "type" : "jonathan" }, "cost" : 1.29 }
{ "_id" : 6, "item" : { "name" : "apple", "type" : "mcintosh" }, "cost" : 1.29 }
{ "_id" : 2, "item" : { "name" : "banana", "type" : "valencia" }, "cost" : 0.99 }

The following query specifies the sort order using the fields from an embedded document item. The query sorts first by the name field in ascending order, and then within each name, by the type field in ascending order.

db.prod_master.find().sort( { "item.name": 1, "item.type": 1 } );

The query returns the following documents, ordered first by the name field, and within each name, by the type field:


> db.prod_master.find().sort( { "item.name": 1, "item.type": 1 } );
{ "_id" : 7, "item" : { "name" : "apple", "type" : "honey crisp" }, "cost" : 1.99 }
{ "_id" : 6, "item" : { "name" : "apple", "type" : "mcintosh" }, "cost" : 1.29 }
{ "_id" : 4, "item" : { "name" : "apple", "type" : "navel" }, "cost" : 1.39 }
{ "_id" : 1, "item" : { "name" : "banana", "type" : "jonathan" }, "cost" : 1.29 }
{ "_id" : 2, "item" : { "name" : "banana", "type" : "valencia" }, "cost" : 0.99 }
{ "_id" : 5, "item" : { "name" : "mango", "type" : "cortland" }, "cost" : 1.29 }
{ "_id" : 9, "item" : { "name" : "mango", "type" : "fuji" }, "cost" : 1.99 }
{ "_id" : 10, "item" : { "name" : "mango", "type" : "jonagold" }, "cost" : 1.29 }
{ "_id" : 8, "item" : { "name" : "orange", "type" : "cara cara" }, "cost" : 2.99 }
{ "_id" : 3, "item" : { "name" : "orange", "type" : "satsuma" }, "cost" : 1.99 }

Retrieve the restaurants data from here

Previous: cursor.skip() method
Next: cursor.toArray() method



Follow us on Facebook and Twitter for latest update.