Skip to main content

db.command.aggregate.ceil

1. Operator Description

Function: Ceiling. Returns the smallest integer greater than or equal to the given number.

Declaration: db.command.aggregate.ceil(expression)

2. Operator Parameters

FieldTypeRequiredDescription
-ExpressionRequiredCan 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 sales contains the following records:

{ _id: 1, sales: 5.2 }
{ _id: 2, sales: 1.32 }
{ _id: 3, sales: -3.2 }

The ceiling value of each number can be obtained as follows:

// 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('sales')
.aggregate()
.project({
sales: $.ceil('$sales')
})
.end()
console.log(res.data)
}

The returned result is as follows:

{ _id: 1, sales: 6 }
{ _id: 2, sales: 2 }
{ _id: 3, sales: -3 }