$currentDate

On this page本页内容

Definition定义

$currentDate

The $currentDate operator sets the value of a field to the current date, either as a Date or a timestamp. $currentDate运算符将字段的值设置为当前日期(作为Datetimestamp)。The default type is Date.默认类型为Date

The $currentDate operator has the form:$currentDate运算符的格式为:

{ $currentDate: { <field1>: <typeSpecification1>, ... } }

<typeSpecification> can be either:可以是以下两种之一:

  • a boolean true to set the field value to the current date as a Date, or布尔值true,用于将字段值设置为当前日期,形式为Date,或
  • 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行为

If the field does not exist, $currentDate adds the field to a document.如果该字段不存在,$currentDate将该字段添加到文档中。

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"字段更新为当前时间戳,并将status字段更新为"D",将"cancellation.reason"更新为"user request"

db.customers.updateOne(
   { _id: 1 },
   {
     $currentDate: {
        lastModified: true,
        "cancellation.date": { $type: "timestamp" }
     },
     $set: {
        "cancellation.reason": "user request",
        status: "D"
     }
   }
)

After the operation, you can query the collection to verify the update:操作完成后,您可以查询集合以验证更新:

db.customers.find().pretty()

The updated document would resemble:更新后的文件类似于:

{
   "_id" : 1,
   "status" : "D",
   "lastModified" : ISODate("2020-01-22T21:21:41.052Z"),
   "cancellation" : {
      "date" : Timestamp(1579728101, 1),
      "reason" : "user request"
   }
}

Aggregation Alternative to $currentDate$currentDate的聚合替代方案

Starting in version 4.2, update methods can accept an aggregation pipeline. 从版本4.2开始更新方法可以接受聚合管道。As such, 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_TIME is available only on replica sets and sharded clusters.仅在副本集和分片群集上可用。
  • The NOW and CLUSTER_TIME values remain the same throughout the pipeline.NOWCLUSTER_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"
   }
}