Definition定义
$currentDateThe$currentDateoperator sets the value of a field to the current date, either as a Date or a timestamp.$currentDate运算符将字段的值设置为当前日期,可以是Date或timestamp。The default type is Date.默认类型为日期。
Compatibility兼容性
You can use 您可以将$currentDate for deployments hosted in the following environments:$currentDate用于在以下环境中托管的部署:
- 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语法
The $currentDate operator has the form:$currentDate运算符的格式为:
{ $currentDate: { <field1>: <typeSpecification1>, ... } }
<typeSpecification> can be either:可以是:
a boolean布尔值trueto set the field value to the current date as a Date, ortrue,将字段值设置为当前日期作为日期,或a document明确指定类型的文档{ $type: "timestamp" }or{ $type: "date" }which explicitly specifies the type.{ $type: "timestamp" }或{ $type: "date" }。The operator is case-sensitive and accepts only the lowercase运算符区分大小写,只接受小写的"timestamp"or the lowercase"date"."timestamp"或小写的"date"。
To specify a 要在嵌入式文档或数组中指定<field> in an embedded document or in an array, use dot notation.<field>,请使用点符号。
Behavior行为
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order. See Update Operators Behavior for details.从MongoDB 5.0开始,更新运算符按字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。有关详细信息,请参阅更新运算符行为。
$currentDate sets the specified field to the date when 将指定字段设置为运行$currentDate was run.$currentDate的日期。
If the field does not exist, 如果该字段不存在,$currentDate adds the field to a document.$currentDate会将该字段添加到文档中。
Starting in MongoDB 5.0, 从MongoDB 5.0开始,当您使用像mongod no longer raises an error when you use an update operator like $currentDate with an empty operand expression ( { } ). An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).$currentDate这样的更新运算符和空操作数表达式({ })时,mongod不再引发错误。空更新不会导致任何更改,也不会创建oplog条目(这意味着该操作是无操作)。
Example示例
Create a sample collection 使用以下文档创建样本集合customers with the following document:customers:
db.customers.insertOne(
{ _id: 1, status: "a", lastModified: ISODate("2013-10-02T01:11:18.965Z") }
)
The following operation updates the 以下操作将lastModified field to the current date, the "cancellation.date" field to the current timestamp as well as updating the status field to "D" and the "cancellation.reason" to "user request".lastModified字段更新为当前日期,将"cancellation.date"字段更新为目前的时间戳,并将状态字段更新为"D",将"cancellation.reason"更新为"user request"。
db.customers.updateOne(
{ _id: 1 },
{
$currentDate: {
lastModified: true,
"cancellation.date": { $type: "timestamp" }
},
$set: {
"cancellation.reason": "user request",
status: "D"
}
}
)
$currentDate sets the specified field to the date when $currentDate was run.$currentDate将指定字段设置为运行$currentDate的日期。
To verify the update:要验证更新,请执行以下操作:
db.customers.find()
The updated document resembles:更新后的文档类似于:
{
"_id" : 1,
"status" : "D",
"lastModified" : ISODate("2020-01-22T21:21:41.052Z"),
"cancellation" : {
"date" : Timestamp(1579728101, 1),
"reason" : "user request"
}
}
The lastModified field is set to the date when $currentDate was run in the update example shown earlier.lastModified字段设置为在前面显示的更新示例中运行$currentDate的日期。
Aggregation Alternative to $currentDate$currentDate的聚合替代方案
$currentDateUpdate methods can accept an aggregation pipeline. Specifically, the previous example can be rewritten as the following using the aggregation stage 更新方法可以接受聚合管道。具体来说,可以使用聚合阶段$set and the aggregation variables NOW (for the current datetime) and CLUSTER_TIME (for the current timestamp):$set和聚合变量NOW(用于当前日期时间)和CLUSTER_TIME(用于当前时间戳)将前面的示例重写如下:
Tip
To access aggregation variables, prefix the variable with double dollar signs要访问聚合变量,请在变量前添加双美元符号$$and enclose in quotes.$$并括在引号中。CLUSTER_TIMEis available only on replica sets and sharded clusters.仅在副本集和分片集群上可用。TheNOWandCLUSTER_TIMEvalues remain the same throughout the pipeline.NOW和CLUSTER_TIME值在整个管道中保持不变。
db.customers.updateOne(
{ _id: 1 },
[
{ $set: { lastModified: "$$NOW", cancellation: {date: "$$CLUSTER_TIME", reason: "user request"}, status: "D" } }
]
)
After the operation, you can query the collection to verify the update:操作完成后,您可以查询集合以验证更新:
db.customers.find().pretty()
The query should return the following document:查询应返回以下文档:
{
"_id" : 1,
"status" : "D",
"lastModified" : ISODate("2020-01-22T21:02:18.994Z"),
"cancellation" : {
"date" : Timestamp(1579726934, 2),
"reason" : "user request"
}
}