Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Learn How to Limit, Skip and Sort Records in MongoDB

In this chapter, we are going to explore limit, skip and sort methods to operate on the documents present inside a collection of MongoDB.

The limit () Method
In MongoDB, the ‘limit ()’ method is used to limit the records or documents present inside a collection. It accepts only one argument which is of number type. Depending on the value of the number, we can limit the number of documents to be displayed. This argument is an optional field inside the ‘limit ()’ method, and when not specified then by default, it will display all the documents from the collection.

The following is the basic syntax for ‘limit ()’ method in MongoDB.

>db.COLLECTION_NAME.find().limit(NUMBER)

Example
The following is an example on using the ‘limit ()’ statement. Let’s insert 4 documents into a collection known as ‘demo_db’.

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Aparajita>cd C:\Program Files\MongoDB\Server\3.2\bin

C:\Program Files\MongoDB\Server\3.2\bin>mongo.exe
MongoDB shell version: 3.2.10
connecting to: test
> use demo_db
switched to db demo_db
> db.demo_db.save ( [ { city : "Toronto" , country : "Canada" }, { city : "Washington D.C." , country : "United States" } , {city : "New Delhi" , country : "India" }, {city : "London" , country : "United Kingdom" } ] )
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 4,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
> db.demo_db.find()
{ "_id" : ObjectId("584c31918dc470c0eabfba95"), "city" : "Toronto", "country" : "Canada" }
{ "_id" : ObjectId("584c31918dc470c0eabfba96"), "city" : "Washington D.C.", "country" : "United States" }
{ "_id" : ObjectId("584c31918dc470c0eabfba97"), "city" : "New Delhi", "country" : "India" }
{ "_id" : ObjectId("584c31918dc470c0eabfba98"), "city" : "London", "country" : "United Kingdom" }
> db.demo_db.find().limit(2)
{ "_id" : ObjectId("584c31918dc470c0eabfba95"), "city" : "Toronto", "country" : "Canada" }
{ "_id" : ObjectId("584c31918dc470c0eabfba96"), "city" : "Washington D.C.", "country" : "United States" }
>

Explanation of the code

  • Here, we are using ‘demo_db’ as the collection name of MongoDB.
  • Next, we are using the ‘save ()’ method to insert four records together as the bulk write operation in the MongoDB.
  • Next, we are using the ‘find ()’ to display all the saved records.
  • Next, we are using the ‘limit (2)’ method with the argument value as 2 which will limit the display of records to two as shown below.

The skip () Method
In MongoDB, the ‘skip ()’ method is used to skip the number of documents. Like the ‘limit ()’ method, it accepts only one argument which is of number type. Depending on the value of the number, we can skip the number of documents to be displayed. This argument is an optional field inside the ‘skip ()’ method, and when not specified, then it will display all documents from the collection as the default value in ‘skip ()’ method is 0.

The following is the basic syntax for ‘skip ()’ method in the MongoDB.

>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)

Example
The following is an example on using the ‘skip ()’ statement.

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Aparajita>cd C:\Program Files\MongoDB\Server\3.2\bin

C:\Program Files\MongoDB\Server\3.2\bin>mongo.exe
MongoDB shell version: 3.2.10
connecting to: test
> use demo_db
switched to db demo_db
> db.demo_db.find()
{ "_id" : ObjectId("584c31918dc470c0eabfba95"), "city" : "Toronto", "country" : "Canada" }
{ "_id" : ObjectId("584c31918dc470c0eabfba96"), "city" : "Washington D.C.", "country" : "United States" }
{ "_id" : ObjectId("584c31918dc470c0eabfba97"), "city" : "New Delhi", "country" : "India" }
{ "_id" : ObjectId("584c31918dc470c0eabfba98"), "city" : "London", "country" : "United Kingdom" }
> db.demo_db.find({},{"city":1,_id:0}).limit(2).skip(1)
{ "city" : "Washington D.C." }
{ "city" : "New Delhi" }
>

Explanation of the code

  • Here, we are using ‘demo_db’ as the collection name of MongoDB.
  • Next, we are using the ‘find ()’ method to display all the saved documents inside this collection.
  • Next, we are using the ‘limit (2)’ along with ‘skip (1) ‘method to make sure only two documents are displayed after skipping one document as shown below.

The sort () Method
In MongoDB, the ‘sort ()’ method is used to sort the documents inside a collection. It accepts a document which contains a list of fields along with their sorting order. We use 1 and -1 in order to specify the sorting order where 1 corresponds to the ascending order and -1 corresponds to the descending order. Also, it should be noted that if we do not specify any sorting preference, then ‘sort ()’ method will display the documents in the ascending order.

The following is the basic syntax for ‘sort ()’ method in the MongoDB.

>db.COLLECTION_NAME.find().sort({KEY:1})

Example
The following is an example on using the ‘sort ()’ statement.

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Aparajita>cd C:\Program Files\MongoDB\Server\3.2\bin

C:\Program Files\MongoDB\Server\3.2\bin>mongo.exe
MongoDB shell version: 3.2.10
connecting to: test
> use demo_db
switched to db demo_db
> db.demo_db.find()
{ "_id" : ObjectId("584c31918dc470c0eabfba95"), "city" : "Toronto", "country" : "Canada" }
{ "_id" : ObjectId("584c31918dc470c0eabfba96"), "city" : "Washington D.C.", "country" : "United States" }
{ "_id" : ObjectId("584c31918dc470c0eabfba97"), "city" : "New Delhi", "country" : "India" }
{ "_id" : ObjectId("584c31918dc470c0eabfba98"), "city" : "London", "country" : "United Kingdom" }
> db.demo_db.find({},{"city":1,_id:0}).sort({"city":-1})
{ "city" : "Washington D.C." }
{ "city" : "Toronto" }
{ "city" : "New Delhi" }
{ "city" : "London" }
> db.demo_db.find({},{"city":1,_id:0}).sort({"city": 1})
{ "city" : "London" }
{ "city" : "New Delhi" }
{ "city" : "Toronto" }
{ "city" : "Washington D.C." }
>

Explanation of the code

  • Here, we are using ‘demo_db’ as the collection name of MongoDB.
  • Next, we are using the ‘find ()’ method to display all the saved documents inside this collection.
  • Next, we are using the ‘sort ({“city”: -1})’ method to display the cities in the descending order as the argument value is -1.
  • Lastly, we are using the ‘sort ({“city”: 1})’ method to display the cities in the ascending order as the argument value is 1.


Conclusion
In this chapter, we have learnt about practical use of limit, skip, and sort methods with the help of numerous examples.

The post Learn How to Limit, Skip and Sort Records in MongoDB appeared first on Eduonix.com | Blog.



This post first appeared on How And When Should You Use HBase NoSQL DB, please read the originial post: here

Share the post

Learn How to Limit, Skip and Sort Records in MongoDB

×

Subscribe to How And When Should You Use Hbase Nosql Db

Get updates delivered right to your inbox!

Thank you for your subscription

×