![]() |
VOOZH | about |
Mongoose, a popular MongoDB ODM (Object Data Modeling) library for Node.js, provides a powerful API to perform advanced aggregation operations. One such method is Aggregate.prototype.exec(), which allows you to execute an aggregation pipeline and return a promise or use a callback to handle the results. This article will explain how to use the exec() method effectively in your Mongoose applications, helping you fetch and manipulate data with ease.
The exec() method in Mongoose’s aggregation API executes the aggregation pipeline on the current model, either returning a promise (for async/await) or accepting a callback function to handle the result. This gives you flexibility in managing database queries, making it easy to handle asynchronous tasks.
Syntax:
aggregate.exec( callback )
callback: The callback function receives two parameters:
error and result.exec() returns a promise that resolves with the aggregation result or rejects with an error.To set up the Mongoose aggregation pipeline with exec(), follow these steps:
Run the following command in your terminal
npm initAfter creating the NodeJS application, Install the required module using the following command:
npm install mongooseThe project structure will look like this:
Database Structure: The database structure will look like this, the following documents are present in the collection.
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 have added pipeline operation to select name field from the collection. And we are calling exec() method to get the result set.
Filename: app.js
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.jsOutput:
[
{ _id: 3, name: 'Ben Stokes' },
{ _id: 2, name: 'David Warner' },
{ _id: 5, name: 'Aaron Finch' },
{ _id: 7, name: 'K L Rahul' },
{ _id: 6, name: 'Hardik Pandya' },
{ _id: 1, name: 'Virat Kohli' },
{ _id: 4, name: 'Rohit Sharma' }
]
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 handling the promise returned by exec() method using asynchronous function and await keyword.
Filename: app.js
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.jsOutput:
[
{ name: 'Ben Stokes', nationality: 'England ' },
{ name: 'David Warner', nationality: 'Australia' },
{ name: 'Aaron Finch', nationality: 'Australia ' },
{ name: 'K L Rahul', nationality: 'India ' },
{ name: 'Hardik Pandya', nationality: 'India ' },
{ name: 'Virat Kohli', nationality: 'India' },
{ name: 'Rohit Sharma', nationality: 'India ' }
]
exec()?The exec() method provides flexibility in how you handle results from the aggregation pipeline. It allows you to:
async/await, exec() makes handling complex queries simpler and more readable.exec()?You should use exec() in the following cases:
Mongoose’s aggregate.exec() method is an essential tool for executing aggregation pipelines and handling results efficiently. Whether you prefer using callback functions or async/await, this method provides a flexible and easy-to-understand way to interact with MongoDB aggregation results. By using exec(), you can easily handle complex aggregation tasks and improve the performance and scalability of your Node.js applications.