Open In App

Mongoose Aggregate.prototype.cursor() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Aggregate API.prototype.cursor() method of the Mongoose API is used to perform aggregation tasks. It allows us to iterate the result set. Aggregation cursor object allows us to perform iteration task on each document object in result set one by one.

Syntax:

aggregate(...).cursor( options )

Parameters: This method accepts a single parameter as discussed below:

  • options: It is an object used to set parameters for the method. This object has a batchSize parameter that can be used to set the batch size of the cursor.

Return Value: It returns cursor object which will represent the current aggregation pipeline operations.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this: 

 

Database Structure: The database structure will look like this, the following documents are present in the collection.

 

Example 1: In this example, we have established a database connection using mongoose and defined model over cricketerSchema, having three columns or fields “_id”, “name”, and “nationality”. At the end, we are calling cursor() method on aggregate object to the reference of cursor. To iterate all the document objects returned by aggregation pipeline we are using eachAsyn() method on cursor object.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const cricketerSchema = new mongoose.Schema({
    _id: Number,
    name: String,
    nationality: String
});
  
const Cricketer = mongoose.model('Cricketers', cricketerSchema);
  
const cursorReference = Cricketer
    .aggregate([{ $project: { name: 1 } }])
    .cursor({ batchSize: 1000 });
  
cursorReference.eachAsync((document, count) => {
    console.log(document, count);
})


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{ _id: 3, name: 'Ben Stokes' } 0
{ _id: 2, name: 'David Warner' } 1 
{ _id: 5, name: 'Aaron Finch' } 2  
{ _id: 7, name: 'K L Rahul' } 3    
{ _id: 6, name: 'Hardik Pandya' } 4
{ _id: 1, name: 'Virat Kohli' } 5  
{ _id: 4, name: 'Rohit Sharma' } 6 

Example 2: In this example, we have established a database connection using mongoose and defined model over cricketerSchema, having three columns or fields “_id”, “name”, and “nationality”. At the end, to get the first document for the current aggregation pipeline we are using next() method on cursor object which is returning first document for current pipeline operation.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const cricketerSchema = new mongoose.Schema({
    _id: Number,
    name: String,
    nationality: String
});
  
const Cricketer = mongoose.model('Cricketers', cricketerSchema);
  
Cricketer
    .aggregate([{ $project: { name: 1 } }])
    .cursor({ batchSize: 1000 })
    .next()
    .then((firstDocument) => {
        console.log(firstDocument)
    });


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{ _id: 3, name: 'Ben Stokes' }

Reference: https://mongoosejs.com/docs/api/aggregate.html#aggregate_Aggregate-cursor



Last Updated : 07 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads