$currentDate

işleci $currentDate , bir alanın değerini tarih veya zaman damgası olarak geçerli tarihe ayarlar. Bu işleç, belgelerin en son ne zaman değiştirildiğini izlemek veya oluşturma zaman damgalarını ayarlamak için kullanışlıdır.

Sözdizimi

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

Parametreler

Parametre Description
field Geçerli tarihe ayarlanacağı alanın adı.
typeSpecification Optional. Tarih değerinin türünü belirtir. (Tarih türü için) veya true zaman damgası türü için olabilir { $type: "timestamp" } . Varsayılan değer ( true Tarih).

Örnekler

Stores koleksiyonundaki bu örnek belgeyi göz önünde bulundurun.

{
  "_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"
  }
}

Örnek 1: Geçerli tarihi ayarlama

Depolama belgesine geçerli tarihi içeren bir lastUpdated alan eklemek için, $currentDate işlecini kullanarak geçerli tarihe sahip alanı Date nesnesi olarak oluşturun.

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

Bu işlem aşağıdaki sonucu döndürür.

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

Örnek 2: Geçerli zaman damgasını ayarlama

Değişiklikleri izlemek için hem tarih alanı hem de zaman damgası alanı eklemek için, true değerine ve typestamp değerine sahip $currentDate işlecini kullanın.

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

Bu işlem aşağıdaki sonucu döndürür

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

Örnek 3: İç içe alanları güncelleştirme

Belge yapısında iç içe yerleştirilmiş alanlar için geçerli tarihi ayarlayın.

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

Bu işlem aşağıdaki sonucu döndürür.

[
  {
    "_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)
    }
  }
]