db.command.aggregate.multiply

聚合操作符。取传入的数字参数相乘的结果。

语法如下:

db.command.aggregate.multiply([<expression1>, <expression2>, ...])

参数可以是任意解析为数字的表达式。

示例

假设集合 fruits 有如下记录:

{ "_id": 1, "name": "apple", "price": 10, "quantity": 100 }
{ "_id": 2, "name": "orange", "price": 15, "quantity": 50 }
{ "_id": 3, "name": "lemon", "price": 5, "quantity": 20 }

求各个水果的的总价值:

const $ = db.command.aggregate
db.collection('fruits').aggregate()
  .project({
    name: 1,
    total: $.multiply(['$price', '$quantity']),
  })
  .end()

返回结果如下:

{ "_id": 1, "name": "apple", "total": 1000 }
{ "_id": 2, "name": "orange", "total": 750 }
{ "_id": 3, "name": "lemo", "total": 100 }