Skip to main content

db.command.aggregate.trunc

1. Operator Description

Function: Truncates a number to an integer.

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

2. Operator Parameters

FieldTypeRequiredDescription
-ExpressionRequiredThe parameter can be any expression that resolves to a number.

3. Sample Code

Suppose the collection scores contains the following records:

{ "_id": 1, "value": 1.21 }
{ "_id": 2, "value": 3.83 }
{ "_id": 3, "value": -4.94 }
// 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
const _ = db.command

exports.main = async (event, context) => {
const res = await db
.collection('scores')
.aggregate()
.project({
int: $.trunc('$value')
})
.end()
console.log(res.data)
}

The returned result is as follows:

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