May 2017
Intermediate to advanced
388 pages
7h 30m
English
On the server:
cartTotal: function() { let total = CartCollection.aggregate([ { $project: {"priceByquantity":{ $multiply: [ "$price", "$quantity" ] } }}, { $group: { "_id": "null", "totalPrice": { $sum: "$priceByquantity" } } } ]); return total; }
Instead of looping in the items, we can use the MongoDB aggregates. With aggregates, we can group multiple documents, perform calculations, and return a result.
To use aggregates with Meteor, we need to install a package to add support for it. I am using meteorhacks:aggregate, which is available in Atmosphere:
>> meteor add meteorhacks:aggregate
We use $project: to generate the priceByquantity key with a value multiplication ...
Read now
Unlock full access