Skip to main content

db.command.aggregate.ln

1. Operator Description

Function: Calculates the natural logarithm of a given number.

Declaration: db.command.aggregate.ln(number)

Notes:

ln is equivalent to log([<number>, Math.E]), where Math.E is the method in JavaScript to get the value of e.

2. Operator Parameters

FieldTypeRequiredDescription
-ExpressionRequired<number> can be any expression that evaluates to a non-negative number.

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 ln(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('ratings')
.aggregate()
.project({
ln: $.ln('$x')
})
.end()
console.log(res.data)
}