Open In App

Mongoose Aggregate.prototype.pipeline() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Aggregate API.prototype.pipeline() method of the Mongoose API is used to perform aggregation tasks. It allows us to get the current pipeline operation object in the form of an array. It is useful to get all the current pipelining operations or pipeline methods we have applied to perform aggregation tasks.

 Syntax:

aggregate().project( specifications ).pipeline();

Parameters: This method does not accept any parameters.

Return Value: This method returns an array of the current pipeline objects.

Setting up Node.js Mongoose Module:

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: 

 

Example 1: In this example, we have established a database connection using mongoose and defined model over userSchema, having three columns or fields “_id”, “name”, and “bornYear”. At the end, we are calling pipleline() method to get the current pipeline operation status.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    bornYear: Number
});
  
const User = mongoose.model('User', userSchema);
  
const currentPipeline = User
    .aggregate([{ $project: { _id: 0, bornYear: 1 } }])
    .pipeline()
console.log(currentPipeline)


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

node app.js

Output:

[ { '$project': { _id: 0, bornYear: 1 } } ]

Example 2: In this example, we have established a database connection using mongoose and defined model over userSchema, having three columns or fields “_id”, “name”, and “bornYear”. In this example, we have two pipeline operations project and sortByCount. By using pipeline() method we are getting result for both the pipeline operations.

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    bornYear: Number
});
  
const User = mongoose.model('User', userSchema);
  
const currentPipeline = User
    .aggregate()
    .project({ name: 1 })
    .sortByCount({ 'bornYear': 1 })
    .pipeline()
console.log(currentPipeline)


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

node app.js

Output:

[ { '$project': { name: 1 } }, { '$sortByCount': { bornYear: 1 } } ]

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



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