-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Labels
Description
Operations like $multiply are confined to the stage it's coded on ($group in this case).
We should extract all operations and allow them to be called from many different places, like on $project phase.
One example from MongoDB with our orders collection:
test> db.orders.aggregate([{ $project: { array: [ '$name', { $multiply: [ '$price', '$quantity'] }] } }])
[
{ _id: 0, array: [ 'Pepperoni', 190 ] },
{ _id: 1, array: [ 'Pepperoni', 400 ] },
{ _id: 2, array: [ 'Pepperoni', 630 ] },
{ _id: 3, array: [ 'Cheese', 180 ] },
{ _id: 4, array: [ 'Cheese', 650 ] },
{ _id: 5, array: [ 'Cheese', 140 ] },
{ _id: 6, array: [ 'Vegan', 170 ] },
{ _id: 7, array: [ 'Vegan', 180 ] }
]And the data:
db.orders.insertMany([ { name: "Pepperoni", size: "small", price: 19, quantity: 10, date: ISODate("2021-03-13T08:14:30Z") }, { name: "Pepperoni", size: "medium", price: 20, quantity: 20, date: ISODate("2021-03-13T09:13:24Z") }, { name: "Pepperoni", size: "large", price: 21, quantity: 30, date: ISODate("2021-03-17T09:22:12Z") }, { name: "Cheese", size: "small", price: 12, quantity: 15, date: ISODate("2021-03-13T11:21:39.736Z") }, { name: "Cheese", size: "medium", price: 13, quantity: 50, date: ISODate("2022-01-12T21:23:13.331Z") }, { name: "Cheese", size: "large", price: 14, quantity: 10, date: ISODate("2022-01-12T05:08:13Z") }, { name: "Vegan", size: "small", price: 17, quantity: 10, date: ISODate("2021-01-13T05:08:13Z") }, { name: "Vegan", size: "medium", price: 18, quantity: 10, date: ISODate("2021-01-13T05:10:13Z") }]);