Skip to main content

db.command.aggregate.exp

1. Operator Description

Function: Raises e (the base of natural logarithms, Euler's number) to the power of n.

Declaration: db.command.aggregate.exp(exponent)

2. Operator Parameters

FieldTypeRequiredDescription
-ExpressionRequiredexponent can be any expression that evaluates to a number. If the expression evaluates to null or refers to a non-existent field, returns null; if it evaluates to NaN, returns NaN.

3. Sample Code

Suppose the collection math contains the following records:

{ _id: 1, exp: 0 }
{ _id: 2, exp: 1 }
{ _id: 3, exp: 2 }
// 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('math')
.aggregate()
.project({
result: $.exp('$exp')
})
.end()
console.log(res.data)
}

The returned result is as follows:

{ _id: 1, result: 1 }
{ _id: 2, result: 2.71828182845905 }
{ _id: 3, result: 7.38905609893065 }