📜  mongodb ISO 日期格式 (1)

📅  最后修改于: 2023-12-03 15:03:01.252000             🧑  作者: Mango

MongoDB ISO Date Format

MongoDB utilizes the ISO date format as a standard for representing dates in the database. The ISO date format is a standardized format for representing dates in a human-readable and machine-readable format. This format is used internationally and is easily parsed by computers.

What is the ISO Date Format?

The ISO date format has the format YYYY-MM-DDTHH:mm:ss.sssZ.

  • YYYY: year (e.g. 2021)
  • MM: month (01-12)
  • DD: day of the month (01-31)
  • T: separates the date and time
  • HH: hour (00-23)
  • mm: minute (00-59)
  • ss: second (00-59)
  • sss: millisecond (000-999)
  • Z: time zone offset from UTC (e.g. +0300)

Example: 2021-07-07T13:00:40.123Z

Why Use ISO Date Format?

The use of the ISO date format in MongoDB has several advantages.

Firstly, it is a standardized format which makes it easy to parse and compare dates across different systems and applications.

Secondly, MongoDB can natively recognize and manipulate dates stored in the ISO format, which makes it faster and more efficient to work with dates in MongoDB.

How to Use ISO Date Format in MongoDB?

MongoDB supports two data types for dates: Date and ISODate.

To create a new date object in ISO format, you can use the ISODate() method:

db.collection.insert({
  "createdAt": ISODate("2021-07-07T13:00:40.123Z")
})

To query and filter based on dates in ISO format, you can use the $date operator:

db.collection.find({
  "createdAt": {
    "$date": "2021-07-07T13:00:40.123Z"
  }
})
Conclusion

The ISO date format in MongoDB provides a standardized and efficient way to store and manipulate dates in the database. When working with MongoDB, it is recommended to use the ISO date format for optimal performance and compatibility.