$currentDate

L'operatore $currentDate imposta il valore di un campo sulla data corrente, come data o timestamp. Questo operatore è utile per tenere traccia dell'ultima modifica dei documenti o dell'impostazione dei timestamp di creazione.

Sintassi

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

Parametri

Parametro Description
field Nome del campo da impostare sulla data corrente.
typeSpecification Optional. Specifica il tipo del valore di data. Può essere true (per tipo di data) o { $type: "timestamp" } per il tipo di timestamp. Il valore predefinito è true (Data).

Esempi

Si consideri questo documento di esempio dalla raccolta negozi.

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

Esempio 1: Impostazione della data corrente

Per aggiungere un lastUpdated campo con la data corrente a un documento dell'archivio, utilizzare l'operatore $currentDate per creare il campo con la data corrente come oggetto Date.

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

Questa operazione restituisce il risultato seguente.

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

Esempio 2: Impostazione del timestamp corrente

Per aggiungere sia un campo data che un campo timestamp per tenere traccia delle modifiche, utilizzare l'operatore $currentDate con un valore true e con un valore typestamp.

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

Questa operazione restituisce il risultato seguente

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

Esempio 3: Aggiornamento dei campi annidati

Impostare la data corrente per i campi annidati nella struttura del documento.

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

Questa operazione restituisce il risultato seguente.

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