Javatpoint Logo
Javatpoint Logo

MongoDB Cursor Methods

The MongoDB cursor methods modifies the way that the specified query is executed. Following are the list of the cursor methods with description, syntax, and examples.

#1 cursor.addOption(flag)

The method adds "OP_QUERY" wire protocol flags. It is added to change the behaviour of queries like tailaible flag.

Example

The above example adds the tailable flag and the awaitData flag to ensure that the query returns a tailable cursor. A cursor will be generated using this method that waits for few seconds after returning the full result set. So that during the query it can get and returns the additional data.

#2. Cursor.batchSize(size)

The batch result from the MongoDB object returns the number of documents which is specified using the batch size method. In many cases, if we modify the batch size, it will not be going to affect the user or the application.

Example


MongoDB Cursor Methods

#3. cursor.close()

The method used to close the cursor and release the associated server resources on the instruction of the method. The cursor will be automatically closed by the server that have zero remaining results or it have been idle for a specified period of time.

Example

#4. cursor.collation(<collation document>)

The MongoDB collation() method specifies the collation for the cursor returned by the db.collection.find().

Collation documents that is accepted by the close method:

Example:

Output:

MongoDB Cursor Methods

#5. cursor.forEach(function)

JavaScript function will be applied to all the documents by the cursor using the forEach method.

Syntax:

Example:

The forEach() method invoked on the cursor returned by find() method to display the name of all the users in the collection:

Output:

MongoDB Cursor Methods

#6. cursor.hint(index)

The method is called during the query to override the MongoDB's default selection of index and query optimization process.

Examples:

All documents in the user's collection using the index on the age field will be returned using the query below.

#7. cursor.limit()

This method is used to specify the maximum number of documents returned by the cursor. It will be used within the cursor and comparable to the LIMIT statement in a SQL database.

Example:

#8. cursor.map(function)

The map method is used by the document visited by the cursor and also collects the return values from nearest application into an array.

Example:

#9. cursor.max()

The max method is used to restrict the results of find().max() method. MongoDB specifies the exclusive upper bound for a specific index that provides a way to specify an upper bound for the compound key indexes.

Example

Create the following indexes for the collection:

If you were using the order of { item: 1, type: 1 } index, the max() restricts the query of the documents that are below the bound of item equal to Mango and type equal to

#10. cursor.min()

To constrain the results of find(). min() MongoDB specifies the lower bound for a specific index in order . This method provides a way to define the lower bounds on compound key indexes.

Syntax:

Example:

First, create a sample collection named as superstore that holds the following documents:

Now, create the indexes for the collection:

MongoDB Cursor Methods

The min() method limits the query to the documents using the ordering of the { item: 1, type: 1 } index.

#11. cursor.tailable()

The tailable method marks the cursor as tailable. It works as a scanner over a capped collection. Even after reaching the last node of the collection it remain open. The application of the method will be continuously running as new data inserted inside the collection.

Syntax:

If the awaitdata flag is set to true, MongoDB blocks the query thread for a period of time when the end of the capped collection was reached by the cursor waiting for new data to arrive. The blocked thread is signaled to wake up and return the next batch to the client when new data is inserted into the capped collection.

#12. cursor.toArray()

The method returns an array that have all the documents belongs to the cursor. It loads all the documents into RAM and exhaust the cursor by iterating the cursor completely.

Example:






Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA