Simuler les recherches de BD
// Insert a few documents into the sales collection.
db.sales.insertMany([
{ '_id': 1, 'item': 'abc', 'price': 10, 'quantity': 2, 'date': new Date('2022-03-01T08:00:00Z') },
{ '_id': 2, 'item': 'jkl', 'price': 20, 'quantity': 1, 'date': new Date('2022-03-01T09:00:00Z') },
{ '_id': 3, 'item': 'xyz', 'price': 5, 'quantity': 10, 'date': new Date('2022-03-15T09:00:00Z') },
{ '_id': 4, 'item': 'xyz', 'price': 5, 'quantity': 20, 'date': new Date('2022-04-04T11:21:39.736Z') },
{ '_id': 5, 'item': 'abc', 'price': 10, 'quantity': 10, 'date': new Date('2022-04-04T21:23:13.331Z') },
{ '_id': 6, 'item': 'def', 'price': 7.5, 'quantity': 5, 'date': new Date('2022-08-04T05:08:13Z') },
{ '_id': 7, 'item': 'def', 'price': 7.5, 'quantity': 10, 'date': new Date('2022-09-10T08:43:00Z') },
{ '_id': 8, 'item': 'abc', 'price': 10, 'quantity': 5, 'date': new Date('2023-02-06T20:20:13Z') },
]);
// Build an aggregation to view total sales for each product in 2022.
const aggregation = [
{ $match: { date: { $gte: new Date('2022-01-01'), $lt: new Date('2022-07-01') } } },
{ $group: { _id: '$item', totalSaleAmount: { $sum: { $multiply: [ '$price', '$quantity' ] } } } }
];
[
{
"_id": "xyz",
"totalSaleAmount": 150
},
{
"_id": "jkl",
"totalSaleAmount": 20
},
{
"_id": "abc",
"totalSaleAmount": 120
}
]