Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


MongoDB Limits, Skips, and Sorts


limit function

The limit() function in MongoDB is used to specify the maximum number of results to be returned. Only one parameter is required for this function.to return the number of the desired result.
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)


If we want to fetch first two documents from the collection "employees", the following mongodb command can be used :


> db.employees.find().limit(2).pretty();


NOTE- find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.



Skip function

skip () method is used to skip the number of documents. 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. Example:


>db.employees.find().skip(2).pretty();
>db.employees.find().limit(2).skip(1).pretty();


Using skip for a small number of documents is fine. For a large number of results, skip can be slow (this is true in nearly every database, not just MongoDB) and should be avoided. Usually you can build criteria into the documents themselves to avoid having to do large skips, or you can calculate the next query based on the result from the previous one.



Sort () Method-

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.


> db.employees.find({},{"employeeId":1,_id:0}).sort({"employeeName": 1})


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.


     << Previous
Next >>