SDK数据库 Command聚合操作符算数操作符


当前第2页 返回上一页

ln 等价于 log([<number>, Math.E]),其中 Math.E 是 JavaScript 获取 e 的值的方法。


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

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

聚合操作符。计算给定数字在给定对数底下的 log 值。

参数

value: Expression[]

[<number>, <base>]

返回值

Object

API 说明

语法如下:

db.command.aggregate.log([<number>, <base>])

<number> 可以是任意解析为非负数字的表达式。<base> 可以是任意解析为大于 1 的数字的表达式。

如果任一参数解析为 null 或指向任意一个不存在的字段,log 返回 null。如果任一参数解析为 NaN,log 返回 NaN。

示例代码

假设集合 curve 有如下记录:

{ _id: 1, x: 1 }
{ _id: 2, x: 2 }
{ _id: 3, x: 3 }

计算 log2(x) 的值:

const $ = db.command.aggregate
db.collection('staff').aggregate()
  .project({
    log: $.log(['$x', 2])
  })
  .end()

返回结果如下:

{ _id: 1, log: 0 }
{ _id: 2, log: 1 }
{ _id: 3, log: 1.58496250072 }

AggregateCommand.log10(value: Expression<number>): Object

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

聚合操作符。计算给定数字在对数底为 10 下的 log 值。

参数

value: Expression<number>

number

返回值

Object

API 说明

语法如下:

db.command.aggregate.log(<number>)

<number> 可以是任意解析为非负数字的表达式。

log10 等同于 log 方法的第二个参数固定为 10。

示例代码

db.command.aggregate.log10

聚合操作符。计算给定数字在对数底为 10 下的 log 值。

语法如下:

db.command.aggregate.log(<number>)

<number> 可以是任意解析为非负数字的表达式。

log10 等同于 log 方法的第二个参数固定为 10。


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

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

聚合操作符。取模运算,取数字取模后的值。

参数

value: Expression[]

[<dividend>, <divisor>]

返回值

Object

API 说明

语法如下:

db.command.aggregate.mod([<dividend>, <divisor>])

第一个数字是被除数,第二个数字是除数。参数可以是任意解析为数字的表达式。

示例代码

假设集合 shopping 有如下记录:

{ _id: 1, bags: 3, items: 5 }
{ _id: 2, bags: 2, items: 8 }
{ _id: 3, bags: 5, items: 16 }

各记录取 items 除以 bags 的余数(items % bags):

const $ = db.command.aggregate
db.collection('shopping').aggregate()
  .project({
    overflow: $.mod(['$items', '$bags'])
  })
  .end()

返回结果如下:

{ _id: 1, log: 2 }
{ _id: 2, log: 0 }
{ _id: 3, log: 1 }

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

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

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

参数

value: Expression[]

[<expression1>, <expression2>, ...]

返回值

Object

API 说明

语法如下:

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 }

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

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

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

参数

value: Expression[]

[<base>, <exponent>]

返回值

Object

API 说明

语法如下:

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

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

示例代码

假设集合 stats 有如下记录:

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

求 x 和 y 的平方和:

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

返回结果如下:

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

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

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

聚合操作符。求平方根。

参数

value: Expression[]

[<number>]

返回值

Object

API 说明

语法如下:

db.command.aggregate.sqrt([<number>])

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

示例代码

假设直角三角形集合 triangle 有如下记录:

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

假设 x 和 y 分别为两直角边,则求斜边长:

const $ = db.command.aggregate
db.collection('triangle').aggregate()
  .project({
    len: $.sqrt([$.add([$.pow(['$x', 2]), $.pow(['$y', 2])])]),
  })
  .end()

返回结果如下:

{ "_id": 1, "len": 3.605551275463989 }
{ "_id": 2, "len": 8.602325267042627 }
{ "_id": 3, "len": 22.360679774997898 }

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

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

聚合操作符。将两个数字相减然后返回差值,或将两个日期相减然后返回相差的毫秒数,或将一个日期减去一个数字返回结果的日期。

参数

value: Expression[]

[<expression1>, <expression2>]

返回值

Object

API 说明

语法如下:

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

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

示例代码

假设集合 scores 有如下记录:

{ "_id": 1, "max": 10, "min": 1 }
{ "_id": 2, "max": 7, "min": 5 }
{ "_id": 3, "max": 6, "min": 6 }

求各个记录的 max 和 min 的差值。:

const $ = db.command.aggregate
db.collection('scores').aggregate()
  .project({
    diff: $.subtract(['$max', '$min'])
  })
  .end()

返回结果如下:

{ "_id": 1, "diff": 9 }
{ "_id": 2, "diff": 2 }
{ "_id": 3, "diff": 0 }

AggregateCommand.trunc(value: Expression<number>): Object

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

聚合操作符。将数字截断为整形。

参数

value: Expression<number>

number

返回值

Object

API 说明

语法如下:

db.command.aggregate.trunc(<number>)

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

示例代码

假设集合 scores 有如下记录:

{ "_id": 1, "value": 1.21 }
{ "_id": 2, "value": 3.83 }
{ "_id": 3, "value": -4.94 }
const $ = db.command.aggregate
db.collection('scores').aggregate()
  .project({
    int: $.trunc('$value')
  })
  .end()

返回结果如下:

{ "_id": 1, "value": 1 }
{ "_id": 2, "value": 3 }
{ "_id": 3, "value": -4 }



标签:微信小程序

返回前面的内容

相关阅读 >>

微信小程序api 背景

微信小程序 运力方使用onorderpreadd

微信小程序 weui快速上手

微信小程序api 蓝牙适配器接口

微信小程序api 录音-开始录音

微信小程序 运算符

微信小程序 小程序使用realmockupdateorder

微信小程序 初始渲染缓存

sdk数据库 command聚合操作符算数操作符

微信小程序api 绘图对坐标轴进行顺时针旋转

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...