WT Exp 08
WT Exp 08
Objective: Develop a script that uses MongoDB's aggregation framework to perform operations
like grouping, filtering, and sorting. For instance, aggregate user data to find the average age of
users in different cities
MongoDB Setup:
{
"name": "Kashif Naseem",
"age": 21,
Code:
(async function ()
try
await client.connect();
console.log("Connected to MongoDB");
const db = client.db(dbName);
const collection = db.collection(collectionName);
const pipeline = [
$group:
},
},
},
];
const results = await collection.aggregate(pipeline).toArray();
${result.totalUsers}`);});
catch (error)
}
finally
{
await client.close();
}
})();
Output:
User Collection: