OPTIONS
翻译或纠错本页面

$currentDate

$currentDate

The $currentDate operator sets the value of a field to the current date, either as a Date or a timestamp. The default type is date.

The $currentDate operator can take as its operand either

  • a boolean true which creates a Date, or
  • a document which explicitly specifies the type, i.e. { $type: "timestamp" } or { $type: "date" }. The operator is case-sensitive and accepts only the lowercase "timestamp" or the lowercase "date".

Example

Consider the following document in the users collection:

{ _id: 1, status: "a", lastModified: ISODate("2013-10-02T01:11:18.965Z") }

The following updates the lastModified field to the current date and the lastModifiedTS field to the current timestamp as well as setting the status field to "D".

db.users.update( { _id: 1 },
                 {
                   $currentDate: {
                                   lastModified: true,
                                   lastModifiedTS: { $type: "timestamp" }
                                 },
                   $set: { status: "D" }
                 }
               )

Following this operation, the updated document would resemble:

{
  _id: 1,
  status: "D",
  lastModified: ISODate("2013-10-02T01:11:53.976Z"),
  lastModifiedTS: Timestamp(1380676313, 1)
}
ON THIS PAGE