Open In App

Mongoose Aggregate.prototype.append() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Aggregate API.prototype.append() method of the Mongoose API is used to perform aggregation tasks. It allows us to update or modify the current pipeline by adding new operations. Using the append() method we can append new pipeline operations to the current aggregation pipeline.

 Syntax:

aggregate(...).append( <Object/ArrayObject> )

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

  • operations: This method takes operations as a parameter in the form of Object or Array of Object.

Return Value: It returns the aggregate result set in the form of array.

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”. First, we have defined initial pipeline operation on project aggregation, then we are printing the current pipeline status. At the end, we have appended new pipeline operation using match aggregation, and in the last using console.log displaying the pipeline status before and after appending the operations along with the aggregation result.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const cricketerSchema = new mongoose.Schema({
    _id: Number,
    name: String,
    nationality: String
});
const Cricketer = mongoose.model(
    'Cricketers', cricketerSchema);
  
const aggregate = Cricketer.aggregate();
  
aggregate.project({ name: 1, nationality: 1 })
  
const beforeAppend = aggregate.pipeline();
  
console.log("Before append - ", beforeAppend);
  
aggregate.append({ $match: { _id: 1 } })
.exec((error, success) => {
    console.log("Result - ", success);
    const afterAppend = aggregate.pipeline();
    console.log("After append - ", afterAppend);
});


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

node app.js

Output:

Before append -  [ { '$project': { 
    name: 1, 
    nationality: 1 
} } ]
Result -  [ { 
    _id: 1, 
    name: 'Virat Kohli', 
    nationality: 'India' 
} ]
After append -  [
  { '$project': { name: 1, nationality: 1 } },
  { '$match': { _id: 1 } }
]

Example 2: In this example, instead of passing an object to the append() method, we are passing array of object which contains pipeline operation.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const cricketerSchema = new mongoose.Schema({
    _id: Number,
    name: String,
    nationality: String
});
const Cricketer = mongoose.model(
    'Cricketers', cricketerSchema);
  
const aggregate = Cricketer.aggregate();
  
aggregate.project({ name: 1, nationality: 1 })
  
const beforeAppend = aggregate.pipeline();
  
console.log("Before append - ", beforeAppend);
  
const appendPipeline = [{ $match: { _id: 5 } }]
  
aggregate.append(appendPipeline).then((result) => {
    console.log("Result - ", result);
    const afterAppend = aggregate.pipeline();
    console.log("After append - ", afterAppend);
});


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

node app.js

Output:

Before append -  [ { '$project': { 
    name: 1, 
    nationality: 1 
} } ]
Result -  [ { 
    _id: 5, 
    name: 'Aaron Finch', 
    nationality: 'Australia ' 
} ]
After append -  [
  { '$project': { name: 1, nationality: 1 } },
  { '$match': { _id: 5 } }
]

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



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