$ceil - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$ceil

4.0 版的新增内容

与 MongoDB 一样,Amazon DocumentDB 中的$ceil运算符将数字向上舍入到最接近的整数。当您需要对数值字段执行数学运算并确保结果为整数时,这很有用。

参数

  • expression: 要向上舍入的数值表达式。

示例(MongoDB 外壳)

此示例演示如何使用$ceil运算符向上舍入数值字段。

创建示例文档

db.numbers.insertMany([ { "_id": 1, "value": 3.14 }, { "_id": 2, "value": -2.7 }, { "_id": 3, "value": 0 } ])

查询示例

db.numbers.aggregate([ { $project: { "roundedUp": { $ceil: "$value" } }} ])

输出

{ "_id": 1, "roundedUp": 4 } { "_id": 2, "roundedUp": -2 } { "_id": 3, "roundedUp": 0 }

代码示例

要查看使用该$ceil命令的代码示例,请选择要使用的语言的选项卡:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('numbers'); const result = await collection.aggregate([ { $project: { "roundedUp": { $ceil: "$value" } }} ]).toArray(); console.log(result); client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test collection = db.numbers result = list(collection.aggregate([ { '$project': { "roundedUp": { '$ceil': "$value" } }} ])) print(result) client.close() example()