Date()
On this page
Date()
-
Returns a date either as a string or as a Date object.以字符串或Date对象的形式返回日期。-
Date()
returns the current date as a string inmongosh
.Date()
在mongosh
中以字符串形式返回当前日期。 -
new Date()
returns the current date as a Date object.将当前日期作为Date
对象返回。mongosh
wraps the Date object with the使用ISODate
helper.ISODate
辅助对象包装Date
对象。TheISODate
is in UTC.ISODate
是协调世界时。
You can specify a particular date by passing an ISO-8601 date string with a year within the inclusive range您可以通过将年份在0到9999之间的ISO-8601日期字符串传递给新的0
through9999
to thenew Date()
constructor or theISODate()
function.date()
构造函数或ISODate()
函数来指定特定日期。These functions accept the following formats:这些函数接受以下格式:-
new Date("<YYYY-mm-dd>")
returns the返回具有指定日期的ISODate
with the specified date.ISODate
。 -
new Date("<YYYY-mm-ddTHH:MM:ss>")
specifies the datetime in the client's local timezone and returns the指定客户端本地时区中的日期时间,并返回指定日期时间(UTC)的ISODate
with the specified datetime in UTC.ISODate
。 -
new Date("<YYYY-mm-ddTHH:MM:ssZ>")
specifies the datetime in UTC and returns the指定以UTC表示的日期时间,并返回以UTC表示指定日期时间的ISODate
with the specified datetime in UTC.ISODate
。 -
new Date(<integer>)
specifies the datetime as milliseconds since the UNIX epoch (Jan 1, 1970), and returns the resulting将日期时间指定为自UNIX epoch(1970年1月1日)以来的毫秒,并返回生成的ISODate
instance.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 epoch(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 }
)
See also: 另请参阅:
-
NOW aggregation variable usageNOW聚合变量用法to update with aggregation pipeline要使用聚合管道进行更新
Return Date as a String字符串形式的返回日期
To return the date as a string, use the 要将日期作为字符串返回,请使用Date()
method, as in the following example:date()
方法,如下例所示:
var myDateString = Date();
Return Date as Date
Object返回日期作为Date
对象
Date
Objectmongosh
wraps objects of Date type with the ISODate
helper; however, the objects remain of type Date.mongosh
使用ISODate
辅助对象包装Date
类型的对象;但是,对象仍然是Date
类型。
The following example uses 以下示例使用new Date()
to return Date object with the specified UTC datetime.new Date()
返回具有指定UTC日期时间的Date
对象。
var myDate = new Date("2016-05-18T16:00:00Z");
See also: 另请参阅:
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:30Z") },
{ _id: 1, type: "strawberry", orderDate: new ISODate("2021-03-20T11:30:05Z") },
{ _id: 2, type: "vanilla", orderDate: new ISODate("2021-01-15T06:31:15Z") }
] )
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.000Z") } } )
Example output:输出示例:
[
{
_id: 0,
type: 'chocolate',
orderDate: ISODate("2020-05-18T14:10:30.000Z")
},
{
_id: 2,
type: 'vanilla',
orderDate: ISODate("2021-01-15T06:31:15.000Z")
}
]