$ceil - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$ceil

バージョン 4.0 の新機能

Amazon DocumentDB の $ceil演算子は、MongoDB と同様に、数値を最も近い整数に切り上げます。これは、数値フィールドで数学演算を実行し、結果が整数であることを確認する必要がある場合に便利です。

パラメータ

  • 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()