Introduction
Welcome back to the MongoDB article series - Part 4. In the previous article, we discussed the different processes of searching data in MongoDB. Now, in this article, we will discuss the advanced search command of a MongoDB Database. If you want to read the previous articles of these series, then follow the links.
In the previous article, we already discussed how to perform basic types of search operations in MongoDB. Now, in this article, we will discuss advanced querying of the data in MongoDB. We can perform any type of ad-hoc queries on the database to find a particular group of data. Also, we can use the range operator for the data query logic. In MongoDB, each query command returns a cursor which basically returns the batch of documents as we provide the conditions of search.
Cursors
When we use the find method in the MongoDB, then it always returns the result as a cursor. With the implementation of the cursor in our query, we can achieve great control over the output result which fetches from the database. We can manipulate the total number of results, can skip some numbers in the results, and sort the results with any combination using the cursor.
To define a cursor in the MongoDB shell, we need to query data on a document and then assign that result into a local variable (in MongoDB, the local variable can be defined by the “var” keyword).
var cursor = db.Employee.find()
The main advantage of defining the cursor is that we can analyze all the results at one time. Since we do not assign the data into a variable, the MongoDB shell will automatically show the documents on the screen. This is normal behavior. But, if we store that result, then we can check the result and represent the output as we want. So, to iterate the data, we can use the next method on the cursor. If we want to check if either variable contains any data or not, then we need to use a hasNext command. Similarly, if we want to move to the next record then we can use the next() method.
Actually, when we call the find method, the MongoDB shell does not communicate with the database for the data. MongoDB shell actually waits until we start to send the request for the result by sending the query. Now, when we execute the command cursor.hasNext() method, then MongoDB shell sends the query to the server. After it, shell fetches the first 100 results or 4 MB result (whichever is smaller size) at a single request so that with next call shell does the same steps to fetch the next set of records. This process continues until the cursor is exhausted or all results have been returned to the query.
In MongoDB, there are several commands that can be used to implement the cursor properly for fetching the data, including some customized operations like sort, skip etc. The below tables displays the list of commands which can be used for implementing the cursor in MongoDB.
| Operator | Descriptions | Example |
| limit | This command is used to limit the number of records during find(). This command sets the upper limit of the records to be displayed. This means if we provide limit value as 5 and total records as per the condition as 4 then all 4 records will be returned by the query. | db.posts.find().limit(5)This command returns document list which returns up to 5 documents list |
| skip | This command works similarly to limit. The only difference is that it will skip the number of records mentioned in the skip method and then return the result of the documents. | db.posts.find().skip(5)This command returns document list in which it will skip the first 5 records and then display the rest of the employees. |
| sort |
This command is used to display the fetch documents in a collection in a specified order. With this method, we can provide two parameters – one is a field name or object name which will be sorted and the second one is order – 1 for ascending and -1 for descending order. Sort command basically maintains the predefined sort order of the display data types
|
db.posts.find().sort({“author”:1})This command returns document list with the ascending order of the system. |

Muthu KumarPosted Sep 9, 2018, 11:51 PM
Nice one..