MongoDB - SPLessons

MongoDB Sorting

Chapter 24

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MongoDB Sorting

MongoDB Sorting

Sorting of records

shape Description

The more obvious and popular cursor option is sorting. In MongoDB Sorting collections will be arranged in ascending or descending order based on the cursor attribute. And these MongoDB Sorting is performed from the server end only.

shape Syntax

The syntax for MongoDB Sorting records is as follows:
>db.collection_name.find().sort({key:1})

Find() – Will find the required records.

Sort record – Will arrange in ascending or descending order

shape Examples

By viewing the below example, one can easily understand the concept of sort().

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.names.find();
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>var cur = db.names.find();
>cur
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>cur.sort({"names": -1});
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}

Here in the above example the sort() will arrange the collection_id in descending order. And these MongoDB Sorting will effects only string names not numbers.

Limiting of records

shape Description

Limit cursor is used for retrieving only specific documents or even all the documents that matching the given criteria.Limit cursor takes a number and returns only that number of documents,if they exist,if less than the limit exists only those would be returned,and the cursor can be made efficient on the server because the server known it doesn’t need to prepare any more documents,it can a creates a much smaller data structures.

shape Syntax

The syntax for limiting of records is as follows:
>db.collection_name.find().limit(number)

Find() – Will find the required records

Limit – Takes a number and return only that number of documents.

shape Examples

By viewing the below example, one can easily understand the concept of limit().

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.names.find();
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>var cur = db.names.find();
>cur
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>var cur =db.names.find();
>cur.limit(3);
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}

Here in the above example the limit() will limit the collections in mongoDB program.And the above program has assigned a limit permission to number(3),so it will give the output values of first three collections.

Summary

shape Key Points

  • MongoDB Sorting – Collections will be arranged in ascending or descending order.
  • Limiting of records – Retrieves a specific document that match the given criteria.