기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
$toInt
버전 4.0의 새로운 기능
Amazon DocumentDB의 $toInt 연산자는 입력 값을 정수 데이터 형식으로 변환하는 데 사용됩니다. 이 연산자는 필드 또는 표현식을 정수로 표현해야 할 때 유용하며, 이는 특정 작업 또는 데이터 처리 작업에 중요할 수 있습니다.
파라미터
예제(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()