Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

วิธี Insert เอกสารพร้อม Date ใน MongoDB

การ Insert เอกสารพร้อมฟิลด์ Date ใน MongoDB

ใช้ new Date() หรือ ISODate() เพื่อสร้าง Date object แล้ว insert ลงในเอกสาร MongoDB จะจัดเก็บเป็น BSON Date (ISODate) อัตโนมัติ

ไวยากรณ์

"yourFieldName": new Date(yourDateValue);

ตัวอย่างการสร้างคอลเลกชันและเพิ่มข้อมูลพร้อม Date

> db.insertDocumentWithDateDemo.insertOne({
    "UserName": "Larry",
    "UserMessage": "Hi",
    "UserMessagePostDate": new Date("2012-09-24")
});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9cca58a629b87623db1b16") }

> db.insertDocumentWithDateDemo.insertOne({
    "UserName": "Chris",
    "UserMessage": "Hello",
    "UserMessagePostDate": new Date("2015-12-31")
});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9cca71a629b87623db1b17") }

> db.insertDocumentWithDateDemo.insertOne({
    "UserName": "Robert",
    "UserMessage": "Bye",
    "UserMessagePostDate": new Date("2019-01-01")
});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9cca85a629b87623db1b18") }

แสดงข้อมูลทั้งหมด

> db.insertDocumentWithDateDemo.find().pretty();

ผลลัพธ์ (จัดเก็บเป็น ISODate UTC):

{ 
    "_id" : ObjectId("5c9cca58a629b87623db1b16"), 
    "UserName" : "Larry", 
    "UserMessage" : "Hi", 
    "UserMessagePostDate" : ISODate("2012-09-24T00:00:00Z") 
}
{ 
    "_id" : ObjectId("5c9cca71a629b87623db1b17"), 
    "UserName" : "Chris", 
    "UserMessage" : "Hello", 
    "UserMessagePostDate" : ISODate("2015-12-31T00:00:00Z") 
}
{ 
    "_id" : ObjectId("5c9cca85a629b87623db1b18"), 
    "UserName" : "Robert", 
    "UserMessage" : "Bye", 
    "UserMessagePostDate" : ISODate("2019-01-01T00:00:00Z") 
}

รูปแบบ Date Input ที่รองรับ

รูปแบบตัวอย่างหมายเหตุ
ISO String (UTC)new Date("2019-01-01")แนะนำ ใช้ได้ทุกที่
ISO String with Timenew Date("2019-01-01T12:30:00Z")ระบุเวลา UTC ชัดเจน
ISO String with Offsetnew Date("2019-01-01T12:30:00+07:00")MongoDB แปลงเป็น UTC อัตโนมัติ
Date Constructornew Date(2019, 0, 1)เดือนเริ่ม 0 (Jan=0) หวังสับสน
Timestamp (ms)new Date(1546300800000)Milliseconds since epoch
ISODate()ISODate("2019-01-01")MongoDB Shell helper เทียบเท่า new Date()

การ Query วันที่

// เท่ากับวันที่ (ทั้งวัน)
db.collection.find({ 
    dateField: { 
        $gte: ISODate("2019-01-01"), 
        $lt: ISODate("2019-01-02") 
    } 
});

// มากกว่าหรือเท่ากับวันที่
db.collection.find({ dateField: { $gte: new Date("2019-01-01") } });

// น้อยกว่าวันที่
db.collection.find({ dateField: { $lt: new Date("2019-12-31") } });

// ระหว่างช่วง
db.collection.find({ 
    dateField: { 
        $gte: ISODate("2019-01-01"), 
        $lte: ISODate("2019-12-31") 
    } 
});

Best Practices

  • ใช้ ISODate String เสมอ (เช่น "2019-01-01" หรือ "2019-01-01T12:00:00Z")
  • หลีกเลี่ยง new Date(year, month, day) เพราะ month เริ่มจาก 0
  • เก็บเป็น UTC เสมอ แปลง Timezone ที่ Application Layer
  • สร้าง Index บนฟิลด์ Date สำหรับ Query ที่ใช้บ่อย
  • ใช้ $dateToString ใน Aggregation เพื่อ Format วันที่แสดงผล

ตัวอย่าง Aggregation กับ Date

db.collection.aggregate([
    { $project: {
        dateString: { 
            $dateToString: { 
                format: "%Y-%m-%d", 
                date: "$dateField",
                timezone: "+07:00"  // แปลงเป็นเวลาประเทศไทย
            } 
        }
    }}
]);