db.command.aggregate.multiply
1. Operator Description
Function: Returns the product of the input numeric parameters.
Declaration: db.command.aggregate.multiply([expression1, expression2, ...])
2. Operator Parameters
Field | Type | Required | Description |
---|---|---|---|
- | Expression | Required | The parameter can be any expression that resolves to a number. |
3. Sample Code
Suppose the collection fruits
contains the following records:
{ "_id": 1, "name": "apple", "price": 10, "quantity": 100 }
{ "_id": 2, "name": "orange", "price": 15, "quantity": 50 }
{ "_id": 3, "name": "lemon", "price": 5, "quantity": 20 }
Calculate the total value of each fruit:
// Sample code in the Cloud Function environment
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})
const db = app.database()
const $ = db.command.aggregate
const _ = db.command
exports.main = async (event, context) => {
const res = await db
.collection('fruits')
.aggregate()
.project({
name: 1,
total: $.multiply(['$price', '$quantity'])
})
.end()
console.log(res.data)
}
The returned result is as follows:
{ "_id": 1, "name": "apple", "total": 1000 }
{ "_id": 2, "name": "orange", "total": 750 }
{ "_id": 3, "name": "lemo", "total": 100 }