AggregateCommand.pow(value: Expression[]): Object

支持端:小程序 2.7.4, 云函数 0.8.1, Web

聚合操作符。求给定基数的指数次幂。

参数

value: Expression[]

[<base>, <exponent>]

返回值

Object

API 说明

语法如下:

  1. db.command.aggregate.pow([<base>, <exponent>])

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

示例代码

假设集合 stats 有如下记录:

  1. { "_id": 1, "x": 2, "y": 3 }
  2. { "_id": 2, "x": 5, "y": 7 }
  3. { "_id": 3, "x": 10, "y": 20 }

xy 的平方和:

  1. const $ = db.command.aggregate
  2. db.collection('stats').aggregate()
  3. .project({
  4. sumOfSquares: $.add([$.pow(['$x', 2]), $.pow(['$y', 2])]),
  5. })
  6. .end()

返回结果如下:

  1. { "_id": 1, "sumOfSquares": 13 }
  2. { "_id": 2, "sumOfSquares": 74 }
  3. { "_id": 3, "sumOfSquares": 500 }