$currentDate

$currentDate演算子は、フィールドの値を日付またはタイムスタンプとして現在の日付に設定します。 この演算子は、ドキュメントが最後に変更された日時を追跡したり、作成タイムスタンプを設定したりする場合に便利です。

構文

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

パラメーター

パラメーター Description
field 現在の日付に設定するフィールドの名前。
typeSpecification Optional. 日付値の型を指定します。 true (日付型の場合) またはタイムスタンプ型の{ $type: "timestamp" }できます。 既定値は true (Date) です。

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
  "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
  "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
  "location": {
    "lat": 60.1441,
    "lon": -141.5012
  },
  "staff": {
    "totalStaff": {
      "fullTime": 2,
      "partTime": 0
    }
  },
  "sales": {
    "salesByCategory": [
      {
        "categoryName": "DJ Headphones",
        "totalSales": 35921
      }
    ],
    "fullSales": 3700
  },
  "promotionEvents": [
    {
      "eventName": "Bargain Blitz Days",
      "promotionalDates": {
        "startDate": {
          "Year": 2024,
          "Month": 3,
          "Day": 11
        },
        "endDate": {
          "Year": 2024,
          "Month": 2,
          "Day": 18
        }
      },
      "discounts": [
        {
          "categoryName": "DJ Turntables",
          "discountPercentage": 18
        },
        {
          "categoryName": "DJ Mixers",
          "discountPercentage": 15
        }
      ]
    }
  ],
  "tag": [
    "#ShopLocal",
    "#SeasonalSale",
    "#FreeShipping",
    "#MembershipDeals"
  ],
  "company": "Lakeshore Retail",
  "city": "Port Cecile",
  "lastUpdated": {
    "$date": "2024-12-11T10:21:58.274Z"
  }
}

例 1: 現在の日付の設定

現在の日付の lastUpdated フィールドをストア ドキュメントに追加するには、$currentDate演算子を使用して、現在の日付を Date オブジェクトとしてフィールドを作成します。

db.stores.updateOne(
  { "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f" },
  {
    $currentDate: {
      "lastUpdated": true
    }
  }
)

この操作は、次の結果を返します。

{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}

例 2: 現在のタイムスタンプの設定

日付フィールドとタイムスタンプ フィールドの両方を追加して変更を追跡するには、値が true で typestamp 値が指定された$currentDate演算子を使用します。

db.stores.updateOne(
  { "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f" },
  {
    $currentDate: {
      "lastModified": true,
      "lastModifiedTimestamp": { $type: "timestamp" }
    }
  }
)

この操作により、次の結果が返されます

{
  acknowledged: true,
  insertedId: null,
  matchedCount: Long("1"),
  modifiedCount: Long("1"),
  upsertedCount: 0
}

例 3: 入れ子になったフィールドの更新

ドキュメント構造の入れ子になったフィールドの現在の日付を設定します。

db.stores.updateOne(
  { "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f" },
  {
    $currentDate: {
      "sales.lastSalesUpdate": true,
      "staff.lastStaffUpdate": { $type: "timestamp" }
    }
  }
)

この操作は、次の結果を返します。

[
  {
    "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
    "name": "First Up Consultants | Bed and Bath Center - South Amir",
    "lastUpdated": ISODate("2025-02-12T10:30:45.123Z"),
    "lastModified": ISODate("2025-02-12T10:30:45.123Z"),
    "lastModifiedTimestamp": Timestamp(1739450445, 1),
    "sales": {
      "totalSales": 37701,
      "lastSalesUpdate": ISODate("2025-02-12T10:30:45.123Z"),
      "salesByCategory": [
        {
          "categoryName": "Mattress Toppers",
          "totalSales": 37701
        }
      ]
    },
    "staff": {
      "totalStaff": {
        "fullTime": 18,
        "partTime": 17
      },
      "lastStaffUpdate": Timestamp(1739450445, 1)
    }
  }
]