$toInt - Amazon DocumentDB

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

$toInt

バージョン 4.0 の新機能

Amazon DocumentDB の $toInt演算子は、入力値を整数データ型に変換するために使用されます。この演算子は、フィールドまたは式が整数として表されるようにする必要がある場合に便利です。これは、特定のオペレーションやデータ処理タスクにとって重要です。

パラメータ

  • expression: 整数に変換される式。

例 (MongoDB シェル)

次の例は、 $toInt演算子を使用して文字列値を整数に変換する方法を示しています。

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

db.numbers.insertMany([ { "name": "one", "value": "1" }, { "name": "hundred", "value": "100" } ]);

クエリの例

db.numbers.aggregate([ { $project: { "_id": 0, "name": 1, "intValue": { $toInt: "$value" } }} ]);

出力

{ "name": "one", "intValue": 1 } { "name": "hundred", "intValue": 100 }

コードの例

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

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: { "_id": 0, "name": 1, "intValue": { $toInt: "$value" } }} ]).toArray(); console.log(result); await 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": { "_id": 0, "name": 1, "intValue": { "$toInt": "$value" } }} ])) print(result) client.close() example()