$second - Amazon DocumentDB

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

$second

Amazon DocumentDB의 $second 연산자는 날짜 또는 타임스탬프에서 초 단위 구성 요소를 추출합니다. 날짜 또는 타임스탬프 필드에서 초 값을 검색하는 데 사용됩니다.

파라미터

  • expression: 초 값을 추출할 날짜 또는 타임스탬프 필드입니다. 이 표현식은 필드 경로이거나 날짜 또는 타임스탬프로 확인되는 유효한 표현식일 수 있습니다.

예제(MongoDB 쉘)

다음 예제에서는 $second 연산자를 사용하여 날짜 필드에서 초 구성 요소를 추출하는 방법을 보여줍니다.

샘플 문서 생성

db.users.insertMany([ { name: "John", dob: new Date("1990-05-15T12:30:45Z") }, { name: "Jane", dob: new Date("1985-09-20T23:59:59Z") }, { name: "Bob", dob: new Date("2000-01-01T00:00:00Z") } ]);

쿼리 예제

db.users.aggregate([{ $project: { name: 1, dobSeconds: { $second: "$dob" } } }])

출력

[ { "_id" : ObjectId("6089a9c306a829d1f8b456a1"), "name" : "John", "dobSeconds" : 45 }, { "_id" : ObjectId("6089a9c306a829d1f8b456a2"), "name" : "Jane", "dobSeconds" : 59 }, { "_id" : ObjectId("6089a9c306a829d1f8b456a3"), "name" : "Bob", "dobSeconds" : 0 } ]

코드 예제

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

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 users = db.collection('users'); const result = await users.aggregate([{ $project: { name: 1, dobSeconds: { $second: '$dob' } } }]).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'] users = db['users'] result = list(users.aggregate([{'$project': {'name': 1, 'dobSeconds': {'$second': '$dob'}}}])) print(result) client.close() example ()