$floor - Amazon DocumentDB

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

$floor

バージョン 4.0 の新機能。

Amazon DocumentDB の $floor演算子は、指定された数以下の最大の整数を返します。この演算子は、数値を四捨五入するのに役立ちます。

パラメータ

  • expression: 切り捨てる数値式。

例 (MongoDB シェル)

次の例は、 $floor演算子を使用して 10 進値を最も近い整数に四捨五入する方法を示しています。

サンプルドキュメントを作成する

db.numbers.insertOne({ value: 3.14 });

クエリの例

db.numbers.aggregate([ { $project: { _id: 0, floored: { $floor: "$value" } } } ]);

出力

{ "floored" : 3 }

コードの例

$floor コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。

Node.js
const { MongoClient } = require('mongodb'); async function example() { const uri = 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'; const client = new MongoClient(uri); try { await client.connect(); const db = client.db('test'); const collection = db.collection('numbers'); const result = await collection.aggregate([ { $project: { _id: 0, floored: { $floor: "$value" } } } ]).toArray(); console.log(result); } catch (error) { console.error('Error:', error); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient from pprint import pprint def example(): client = None try: 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': { '_id': 0, 'floored': { '$floor': '$value' }}} ])) pprint(result) except Exception as e: print(f"An error occurred: {e}") finally: if client: client.close() example()