Grokking MongoDB
Vote

0% completed

​

Arithmetic Operators

After the setup below, one command returns a value that no document stores: how much each product's stock is worth.

db.products.drop() db.products.insertMany([ { name: "Product A", price: 10, quantity: 100 }, { name: "Product B", price: 20, quantity: 150 }, { name: "Product C", price: 15, quantity: 200 }, { name: "Product D", price: 25, quantity: 50 } ])
db.products.aggregate([ { $project: { _id: 0, name: 1, stockValue: { $multiply: ["$price", "$quantity"] } } } ])
[ { name: 'Product A', stockValue: 1000 },

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content