View a markdown version of this page

$dateToParts - Amazon DocumentDB

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

$dateToParts

버전 5.0.1 및 8.0의 새로운 기능

Amazon DocumentDB의 $dateToParts 집계 연산자는 연도, 월, 일, 시간, 분, 초, 밀리초와 같은 날짜 및 시간 값의 구성 요소가 포함된 문서를 반환합니다.

파라미터

  • date: 여러 부분으로 나눌 날짜 및 시간 값입니다.

  • timezone: (선택 사항) 날짜 부분을 추출할 때 사용할 시간대입니다. 지정하지 않으면 파트가 UTC로 반환됩니다.

  • iso8601: (선택 사항) 로 설정하면 반환true된 문서는 ISO 8601 날짜 부분(isoWeekYear, isoWeek, )을 사용합니다isoDayOfWeek. 기본값은 false입니다.

예제(MongoDB 쉘)

다음 예제에서는 $dateToParts 연산자를 사용하여 날짜의 개별 부분을 반환하는 방법을 보여줍니다.

샘플 문서 생성

db.events.insertMany([ { _id: 1, eventDate: ISODate("2023-04-01T10:30:15Z") }, { _id: 2, eventDate: ISODate("2023-12-25T18:45:00Z") } ]);

쿼리 예제

db.events.aggregate([ { $project: { _id: 1, parts: { $dateToParts: { date: "$eventDate" } } } } ])

출력

[ { "_id": 1, "parts": { "year": 2023, "month": 4, "day": 1, "hour": 10, "minute": 30, "second": 15, "millisecond": 0 } }, { "_id": 2, "parts": { "year": 2023, "month": 12, "day": 25, "hour": 18, "minute": 45, "second": 0, "millisecond": 0 } } ]

코드 예제

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

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('events'); const result = await collection.aggregate([ { $project: { _id: 1, parts: { $dateToParts: { date: "$eventDate" } } } } ]).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['events'] result = list(collection.aggregate([ { '$project': { '_id': 1, 'parts': { '$dateToParts': { 'date': '$eventDate' } } } } ])) print(result) client.close() example()