-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day26.js
39 lines (29 loc) · 879 Bytes
/
Day26.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydb', { useNewUrlParser: true, useUnifiedTopology: true });
// Define the Product schema
const productSchema = new mongoose.Schema({
name: String,
price: Number,
quantity: Number,
});
const Product = mongoose.model('Product', productSchema);
function getProductStatistics() {
return Product.aggregate([
{
$group: {
_id: null,
totalProducts: { $sum: 1 },
averagePrice: { $avg: '$price' },
highestQuantity: { $max: '$quantity' },
},
},
]);
}
getProductStatistics().then((result) => {
console.log('Product Statistics:', result);
// Close the MongoDB connection after executing the pipeline
mongoose.connection.close();
}).catch((err) => {
console.error('Error executing pipeline:', err);
mongoose.connection.close();
});