Practical-8 Resource
Practical-8 Resource
Objective
Develop a script that uses MongoDB aggregation framework to perform the operations like
grouping filtering and sorting
Theory
The MongoDB Aggregation Framework is a powerful tool for processing and transforming
data stored in MongoDB collections. It allows developers to create complex data pipelines,
similar to those found in traditional relational databases, but with the flexibility and
scalability of NoSQL databases.
Code
// Connect to the MongoDB server and select the database
try {
await client.connect();
const pipeline = [
$match: {
},
// Group stage to group documents by item and calculate total quantity and total price
$group: {
_id: "$item",
totalQuantity: { $sum: "$quantity" },
},
$sort: { totalQuantity: -1 }
];
console.log(result);
} finally {
await client.close();
run().catch(console.dir);
Output
[