db.command.aggregate.log
1. Operator Description
Function: Computes the logarithm of a given number to a given base.
Syntax: db.command.aggregate.log([number, base])
2. Operator Parameters
Field | Type | Required | Description |
---|---|---|---|
- | <Array>Expression | Yes | <number> can be any expression that resolves to a non-negative number. <base> can be any expression that resolves to a number greater than 1. |
Notes:
If any argument resolves to
null
or points to a missing field,log
returnsnull
. If any argument resolves toNaN
,log
returnsNaN
.
3. Sample Code
Suppose the collection staff
contains the following records:
{ _id: 1, x: 1 }
{ _id: 2, x: 2 }
{ _id: 3, x: 3 }
Calculate the value of log2(x)
:
// 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
exports.main = async (event, context) => {
const res = await db
.collection('staff')
.aggregate()
.project({
log: $.log(['$x', 2])
})
.end()
console.log(res.data)
}
The returned result is as follows:
{ _id: 1, log: 0 }
{ _id: 2, log: 1 }
{ _id: 3, log: 1.58496250072 }