การดึงส่วนประกอบของ Date/ISODate (Year, Month, Day, Hour, Minute, Second) ใน MongoDB
MongoDB จัดเก็บ Date เป็น BSON Date (ISODate) ซึ่งเป็น UTC Timestamp สามารถดึงส่วนประกอบต่างๆ ได้ผ่าน JavaScript Date Methods ใน Mongo Shell หรือใช้ Aggregation Operators
ตัวอย่างสร้างข้อมูล Date
> db.componentOfDateDemo.insert({ "ShippingDate": new Date() });
WriteResult({ "nInserted" : 1 })
แสดงข้อมูล
> db.componentOfDateDemo.find().pretty()
ผลลัพธ์:
{
"_id" : ObjectId("5c9e9d57d628fa4220163b68"),
"ShippingDate" : ISODate("2019-03-29T22:33:59.776Z")
}
ดึง Date Object มาเก็บใน Variable
> var result = db.componentOfDateDemo.findOne();
> result.ShippingDate;
ISODate("2019-03-29T22:33:59.776Z")
วิธีที่ 1: JavaScript Date Methods (ใน Mongo Shell)
กด Tab 2 ครั้งหลัง result.ShippingDate. จะแสดง Methods ทั้งหมด
ผลลัพธ์ Methods ที่ใช้ได้:
> result.ShippingDate.getFullYear()
2019
> result.ShippingDate.getSeconds()
59
> result.ShippingDate.getMinutes()
33
> result.ShippingDate.getHours()
22
> result.ShippingDate.getDate()
29
> result.ShippingDate.getMonth()
2 // 0-based: 0=Jan, 2=March
วิธีที่ 2: Aggregation Date Operators (แนะนำสำหรับ Production)
ใช้ใน Aggregation Pipeline สำหรับประมวลผลฝั่ง Server
Date Extraction Operators
db.componentOfDateDemo.aggregate([
{ $project: {
_id: 0,
ShippingDate: 1,
year: { $year: "$ShippingDate" },
month: { $month: "$ShippingDate" },
day: { $dayOfMonth: "$ShippingDate" },
hour: { $hour: "$ShippingDate" },
minute: { $minute: "$ShippingDate" },
second: { $second: "$ShippingDate" },
millisecond: { $millisecond: "$ShippingDate" },
dayOfWeek: { $dayOfWeek: "$ShippingDate" }, // 1=Sun, 7=Sat
dayOfYear: { $dayOfYear: "$ShippingDate" },
week: { $week: "$ShippingDate" }
}}
]);
ผลลัพธ์:
{
"ShippingDate" : ISODate("2019-03-29T22:33:59.776Z"),
"year" : 2019,
"month" : 3,
"day" : 29,
"hour" : 22,
"minute" : 33,
"second" : 59,
"millisecond" : 776,
"dayOfWeek" : 6,
"dayOfYear" : 88,
"week" : 13
}
การ Format วันที่ (Date Formatting)
// MongoDB 3.6+ : $dateToString
db.componentOfDateDemo.aggregate([
{ $project: {
_id: 0,
formatted: {
$dateToString: {
format: "%Y-%m-%d %H:%M:%S",
date: "$ShippingDate",
timezone: "+07:00" // Thailand timezone
}
}
}}
]);
// ผลลัพธ์: "2019-03-30 05:33:59" (UTC+7)
Format Specifiers สำหรับ $dateToString
| Specifier | ความหมาย | ตัวอย่าง |
|---|---|---|
| %Y | Year (4 digits) | 2019 |
| %m | Month (01-12) | 03 |
| %d | Day (01-31) | 29 |
| %H | Hour 24h (00-23) | 22 |
| %M | Minute (00-59) | 33 |
| %S | Second (00-59) | 59 |
| %L | Millisecond (000-999) | 776 |
| %j | Day of year (001-366) | 088 |
| %w | Day of week (0-6) 0=Sun | 5 |
Timezone Handling
- MongoDB เก็บ Date เป็น UTC เสมอ
- ใช้
timezoneใน $dateToString เพื่อแปลงแสดงผล (เช่น "+07:00" สำหรับไทย) - Aggregation Operators ($year, $month, etc.) ทำงานใน UTC โดยค่าเริ่มต้น
- ระบุ
timezoneใน Operator ได้:{ $hour: { date: "$date", timezone: "+07:00" } }
ภาพประกอบ Methods ใน Shell

สรุป
- Mongo Shell (Ad-hoc): ใช้ JavaScript Date Methods (getFullYear(), getMonth(), etc.)
- Application/Production: ใช้ Aggregation Date Operators ($year, $month, $dateToString, etc.)
- Timezone: ระบุ timezone ชัดเจนเมื่อแสดงผลให้ User