기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
$tsSecond
버전 8.0.1에서 새로 추가되었습니다.
Amazon DocumentDB의 $tsSecond 연산자는 타임스탬프 값의 초 부분을 긴 정수(Unix epoch 초)로 반환합니다.
파라미터
예제(MongoDB 쉘)
다음 예제에서는 $tsSecond 연산자를 사용하여 타임스탬프 값에서 초 부분을 추출하는 방법을 보여줍니다.
샘플 문서 생성
db.events.insertMany([
{_id: 1, ts: Timestamp(1678900000, 1)},
{_id: 2, ts: Timestamp(1678900000, 2)},
{_id: 3, ts: Timestamp(1678900001, 1)}
]);
쿼리 예제
db.events.aggregate([
{ $project: { seconds: { $tsSecond: "$ts" } } }
]);
출력
[
{_id: 1, seconds: Long("1678900000")},
{_id: 2, seconds: Long("1678900000")},
{_id: 3, seconds: Long("1678900001")}
]
코드 예제
$tsSecond 연산자 사용에 대한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.
- Node.js
-
const { MongoClient, Timestamp } = require('mongodb');
async function example() {
const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
try {
await client.connect();
const db = client.db('test');
const collection = db.collection('events');
const result = await collection.aggregate([
{ $project: { seconds: { $tsSecond: "$ts" } } }
]).toArray();
console.log(result);
} finally {
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')
try:
db = client['test']
collection = db['events']
result = list(collection.aggregate([
{'$project': {'seconds': {'$tsSecond': '$ts'}}}
]))
print(result)
finally:
client.close()
example()