In this tutorial we will learn about how to limit documents, skip some documents and using sorting techniques in mongodb.
Retrieve the document from the database
Note: We are using testDB database, and from the previous tutorial we are using collection as testCollection
1) Open a new command Prompt
2) Enter “mongod
“, and press ENTER (it will start your mongodb server. Don’t close it)
3) Open Robo3T.
4) Right click on Connection name.
5) Open shell.
6)Write “use testDB
“, and press CTRL+ENTER. It will switch to testDB database.
7) Write “db.testCollection.find({})
” , and press CTRL+ENTER.
Output Below
First element as Name: SHIP, Value: F, Price: 50
Second element as Name: BIKE, Value: B, Price: 40
Third element as Name: Truck, Value: E, Price: 35
Fourth element as Name: Bicycle, Value: D, Price: 25
Fifth element as Name: Car, Value: A, Price: 20
Sixth element as Name: MOTORCYCLE, Value: C, Price: 10
Limit in MongoDB
Limit keyword in MongoDB limits the quantity of the output. It means suppose you want only 5 outputs after you execute the mongodb query in the shell.
Mongodb command to use limit is:
“db.collectionName.find({}).limit(x)
”
x would be any number starting from 1 to quantity of maximum documents in a collection.
Example: Retrieve only 3 documents with their Name and Price from the testCollection.
MongoDB command is:
“db.testCollection.find({},{“Name”:1, “Price”:1}).limit(3)
”
Write MongoDB command in shell and press CTRL+ENTER.
It will display:
First element as Name : Car, Price:20
Second element as Name: BIKE, Price:40
Third element as Name : MOTORCYCLE, Price:10
Skip in MongoDB
Skip keyword in MongoDB skips the number of documents from the output and display rest from skip. It means suppose you want to skip 3 documents after you execute the mongodb query in the shell.
Mongodb command to use skip is:
“db.collectionName.find({}).skip(x)
”
x would be any number starting from 1 to quantity of maximum documents in a collection.
Example: Retrieve all documents with their Name and Price from the testCollection starting from 4th position.
MongoDB command is:
“db.testCollection.find({},{“Name”:1, “Price”:1}). skip(3)
”
Write MongoDB command in shell and press CTRL+ENTER.
It will display:
Fourth element as Name : Bicycle, Price:25
Fifth element as Name : Truck, Price:35
Sixth element as Name : SHIP, Price:50
Skip and Limit Together in MongoDB
Mongodb command to use skip and limit together is: “db.collectionName.find({}).limit(x).skip(y)
”
x and y would be any number starting from 1 to quantity of maximum documents in a collection.
Example: Retrieve only 2 documents with their Name and Price from the testCollection starting from 5th document.
MongoDB command is:
“db.testCollection.find({},{“Name”:1, “Price”:1}) .limit(2).skip(4)
”
Write MongoDB command in shell and press CTRL+ENTER.
It will display:
Fifth element as Name: Truck, Price: 35
Sixth element as Name: SHIP, Price: 50
Sorting In MongoDB
Sorting generally uses for rearrange the documents in the increasing (ascending) or decreasing (descending) order based on a value.
Mongodb command for sorting is:
“db.collectionName.find({}).sort({“basedValue”:x})
”
x would be 1 or -1. If the sorting order is ascending it would be 1 otherwise if it is descending it is -1.
basedValue denotes the field name on the basis of that the mongodb query sorts.
Example:
A. Sort the documents from the testCollection on the basis of their price in ascending order.
Mongodb command will be:
” db.testCollection.sort({“Price”: 1})
“
Write MongoDB command in shell and press CTRL+ENTER.
It will display:
First element as Name: MOTORCYCLE, Value: C, Price: 10
Second element as Name: Car, Value: A, Price: 20
Third element as Name: Bicycle, Value: D, Price: 25
Fourth element as Name: Truck, Value: E, Price: 35
Fifth element as Name: BIKE, Value: B, Price: 40
Sixth element as Name: SHIP, Value: F, Price: 50
B. Sort the documents from the testCollection on the basis of their price in descending order.
Mongodb command will be: “db.testCollection.sort({“Price”: -1})
”
Write MongoDB command in shell and press CTRL+ENTER.
It will display:
First element as Name: SHIP, Value: F, Price: 50
Second element as Name: BIKE, Value: B, Price: 40
Third element as Name: Truck, Value: E, Price: 35
Fourth element as Name: Bicycle, Value: D, Price: 25
Fifth element as Name: Car, Value: A, Price: 20
Sixth element as Name: MOTORCYCLE, Value: C, Price: 10
If you have any queries regarding limit, skip and sorting documents in MongoDB, drop a comment below.