Date()Returns a date either as a string or as a Date object. The date can contain a date and a time, known as a datetime.以字符串或Date对象的形式返回日期。日期可以包含日期和时间,称为日期时间。The BSON specification states aBSON规范规定Dateobject contains the UTC datetime. UTC is Coordinated Universal Time.Date对象包含UTC日期时间。UTC是协调世界时。The UTC datetime stores an unsigned 64-bit integer value, indicating the number of milliseconds after the Unix epoch (January 1st, 1970 at 00:00:00 UTC).UTC日期时间存储一个无符号的64位整数值,表示Unix纪元(1970年1月1日00:00:00 UTC)后的毫秒数。
Compatibility兼容性
You can use 您可以将Date() for deployments hosted in the following environments:Date()用于在以下环境中托管的部署:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
You can specify either of the following formats:您可以指定以下格式之一:
Date()returns the current date as a string inmongosh.Date()以mongosh字符串形式返回当前日期。new Date()returns the current date as a Date object.将当前日期作为Date对象返回。mongoshwraps the Date object with theISODatehelper.mongosh用ISODate辅助程序包装Date对象。TheISODateis in UTC.ISODate采用UTC。
You can specify a particular date by passing an ISO-8601 date string with a year within the inclusive range 您可以通过将一个年份在0 through 9999 to the new Date() constructor or the ISODate() function. These functions accept the following formats:0到9999范围内的ISO-8601日期字符串传递给新的date()构造函数或ISODate()函数来指定特定日期。这些函数接受以下格式:
new Date("<YYYY-mm-dd>")returns the返回具有指定日期的ISODatewith the specified date.ISODate。new Date("<YYYY-mm-ddTHH:MM:ss.sss>")specifies the datetime in the client's local timezone and returns the指定客户端本地时区中的日期时间,并返回具有指定UTC日期时间的ISODatewith the specified datetime in UTC.ISODate。ss.sssspecifies seconds (指定秒(ss) and milliseconds (.sss). Milliseconds are set to0if omitted.ss)和毫秒(sss)。如果省略,毫秒设置为0。new Date("<YYYY-mm-ddTHH:MM:ss.sssZ>")specifies the datetime in UTC and returns the指定UTC格式的日期时间,并返回具有UTC格式的指定日期时间的ISODatewith the specified datetime in UTC.ISODate。ss.sssspecifies seconds (指定秒(ss) and milliseconds (.sss). Milliseconds are set to0if omitted.ss)和毫秒(.sss)。如果省略,毫秒设置为0。new Date(<integer>)specifies the datetime as milliseconds since the UNIX epoch (Jan 1, 1970), and returns the resulting将日期时间指定为自UNIX纪元(1970年1月1日)以来的毫秒,并返回结果ISODateinstance.ISODate实例。
Behavior行为
Internally, Date objects are stored as a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970).在内部,Date对象存储为一个有符号的64位整数,表示自Unix纪元(1970年1月1日)以来的毫秒数。
Not all database operations and drivers support the full 64-bit range. You may safely work with dates with years within the inclusive range 并非所有数据库操作和驱动程序都支持完整的64位范围。您可以安全地使用0 through 9999.0到9999之间包含年份的日期。
Examples示例
Use Date in a Query在查询中使用日期
If no document with 如果_id equal to 1 exists in the products collection, the following operation inserts a document with the field dateAdded set to the current date:products集合中不存在_id等于1的文档,则以下操作将插入一个字段dateAdded设置为当前日期的文档:
db.products.updateOne(
{ _id: 1 },
{
$set: { item: "apple" },
$setOnInsert: { dateAdded: new Date() }
},
{ upsert: true }
)
Tip
$currentDateNOW aggregation variable usageNOW聚合变量使用to update with aggregation pipeline使用聚合管道进行更新
Return Date as a String返回日期为字符串
To return the date as a string, use the 要以字符串形式返回日期,请使用Date() method. For example:date()方法。例如:
var myDateString = Date();Return Date as Date Object返回日期作为Date对象
Date Objectmongosh wraps objects of Date type with the 使用ISODate helper. ISODate辅助程序包装Date类型的对象。However, the objects are Date types.但是,这些对象是Date类型。
The following example uses 以下示例使用new Date() to return a Date object with the specified UTC datetime:new Date()返回具有指定UTC日期时间的Date对象:
var myDate = new Date("2016-05-18T16:00:00Z");
Insert and Return ISODate Objects插入并返回ISODate对象
ISODate ObjectsYou can specify dates as 您可以将日期指定为ISODate objects.ISODate对象。
The following example creates a 以下示例在cakeSales collection with ISODate objects in the orderDate field:orderDate字段中创建了一个包含ISODate对象的cakeSales集合:
db.cakeSales.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new ISODate("2020-05-18T14:10:30.123Z") },
{ _id: 1, type: "strawberry", orderDate: new ISODate("2021-03-20T11:30:05Z") },
{ _id: 2, type: "vanilla", orderDate: new ISODate("2021-01-15T06:31:15.456Z") }
] )
The following example returns documents where the 以下示例返回orderDate is less than the ISODate specified in the $lt operator:orderDate小于$lt运算符中指定的ISODate的文档:
db.cakeSales.find( { orderDate: { $lt: ISODate("2021-02-25T10:03:46.234Z") } } )
Example output:示例输出:
[
{
_id: 0,
type: 'chocolate',
orderDate: ISODate("2020-05-18T14:10:30.123Z")
},
{
_id: 2,
type: 'vanilla',
orderDate: ISODate("2021-01-15T06:31:15.456Z")
}
]