$toLong - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$toLong

버전 4.0의 새로운 기능

Amazon DocumentDB의 $toLong 연산자는 값을 64비트 정수(긴) 데이터 형식으로 변환하는 데 사용됩니다. 이는 문자열 또는 기타 데이터 형식으로 저장될 수 있는 숫자 값에 대한 산술 연산 또는 비교를 수행해야 할 때 유용할 수 있습니다.

파라미터

  • expression: 64비트 정수로 변환할 표현식입니다.

예제(MongoDB 쉘)

이 예제에서는 $toLong 연산자를 사용하여 문자열 값을 64비트 정수로 변환하는 방법을 보여줍니다.

샘플 문서 생성

db.numbers.insertMany([ { _id: 1, value: "42" }, { _id: 3, value: "9223372036854775807" } ]);

쿼리 예제

db.numbers.aggregate([ { $project: { _id: 1, longValue: { $toLong: "$value" } } } ])

출력

[ { "_id" : 1, "longValue" : 42 }, { "_id" : 3, "longValue" : 9223372036854775807 } ]

코드 예제

$toLong 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

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 numbers = db.collection('numbers'); const result = await numbers.aggregate([ { $project: { _id: 1, longValue: { $toLong: "$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 numbers = db.numbers result = list(numbers.aggregate([ { '$project': { '_id': 1, 'longValue': { '$toLong': '$value' } } } ])) print(result) client.close() example()