$currentDate Operator in MongoDB – Basic Guide

CurrentDate Operator Featured Image

In this guide, I will explain how to use the $currentDate operator in MongoDB.

The $currentDate operator in MongoDB allows users to set a field’s value as the current date. The value by default is of Date type however, you can even set a timestamp type value by explicitly stating it.

This guide will walk you through the easiest way to set a current date value using the $currentDate operator in MongoDB. So, let us get this amazing guide started!

The $currentDate Operator Syntax in MongoDB

 Let us take a quick look at the $currentDate syntax in MongoDB when using in the mongo shell.

{ $currentDate: { field1: typeSpecification1, … } }

We must set the $currentDate as true to allow the operator to set the current date as its operator. To specify the type of date value, you can state as { $type: “timestamp” } or { $type: “date” } as per your needs. Make sure you only use lowercase letters as the operator is case sensitive.

Also, if a given field does not exist, the operator adds it by itself and sets the value as the current date.

Using the $currentDate Operator in MongoDB

Let us get started with using the $currentDate operator in MongoDB.

Before we proceed let me list out all the documents of cats collection that I am going to work with.

> db.cats.find().pretty()
{
        "_id" : ObjectId("602eb6c728ff814b64eb3411"),
        "name" : "Stephanie",
        "age" : 22,
        "breed" : "Persian Cat",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : true
        },
        "moreDetails" : {
                "cost" : 30000
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602eb73728ff814b64eb3412"),
        "name" : "Whiskers",
        "age" : 4,
        "breed" : "Scottish Fold",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : true
        },
        "moreDetails" : {
                "cost" : 30000
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602eb84628ff814b64eb3413"),
        "name" : "Krissy",
        "age" : 6,
        "breed" : "Turkish Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebc5ea78db75e09057cd0"),
        "name" : "Cassy",
        "age" : 16,
        "breed" : "Japanese Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebc9da78db75e09057cd1"),
        "name" : "Shane",
        "age" : 11,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebed7a78db75e09057cd2"),
        "name" : "Phoebe",
        "age" : 21,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}

This is the collection I am going to work with.

Using the $currentDate Operator with a Non-Existent Embedded Field

In the example, I want to insert an embedded document that doesn’t exist in our collection. Let’s say I want to set an adoptionDate field inside the moreDetails field because all our cats got adopted today:

> db.cats.updateMany({}, {$currentDate: {"moreDetails.adoptionDate": true}})
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
> db.cats.find().pretty()
{
        "_id" : ObjectId("602eb6c728ff814b64eb3411"),
        "name" : "Stephanie",
        "age" : 22,
        "breed" : "Persian Cat",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : true
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.933Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602eb73728ff814b64eb3412"),
        "name" : "Whiskers",
        "age" : 4,
        "breed" : "Scottish Fold",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : true
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.938Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602eb84628ff814b64eb3413"),
        "name" : "Krissy",
        "age" : 6,
        "breed" : "Turkish Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.938Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebc5ea78db75e09057cd0"),
        "name" : "Cassy",
        "age" : 16,
        "breed" : "Japanese Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.938Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebc9da78db75e09057cd1"),
        "name" : "Shane",
        "age" : 11,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.939Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebed7a78db75e09057cd2"),
        "name" : "Phoebe",
        "age" : 21,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.939Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}

Setting a Date or Timestamp Using the $currentDate Operator

We will set a date type in this example.

Setting a Date type

> db.cats.updateMany({}, {$currentDate: {"moreDetails.adoptionDate": {$type: "date"}}})

{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
> db.cats.find().pretty()
{
        "_id" : ObjectId("602eb6c728ff814b64eb3411"),
        "name" : "Stephanie",
        "age" : 22,
        "breed" : "Persian Cat",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : true
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.933Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602eb73728ff814b64eb3412"),
        "name" : "Whiskers",
        "age" : 4,
        "breed" : "Scottish Fold",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : true
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.938Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602eb84628ff814b64eb3413"),
        "name" : "Krissy",
        "age" : 6,
        "breed" : "Turkish Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.938Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebc5ea78db75e09057cd0"),
        "name" : "Cassy",
        "age" : 16,
        "breed" : "Japanese Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.938Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebc9da78db75e09057cd1"),
        "name" : "Shane",
        "age" : 11,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.939Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebed7a78db75e09057cd2"),
        "name" : "Phoebe",
        "age" : 21,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : ISODate("2021-10-28T21:47:06.939Z")
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}

The output doesn’t seem any different as the Date type is the default and hence the $currentDate operator spits the same value.

Setting a Timestamp Type

> db.cats.updateMany({}, {$currentDate: {"moreDetails.adoptionDate": {$type: "timestamp"}}})
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
> db.cats.find().pretty()
{
        "_id" : ObjectId("602eb6c728ff814b64eb3411"),
        "name" : "Stephanie",
        "age" : 22,
        "breed" : "Persian Cat",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : true
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : Timestamp(1635457867, 1)
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602eb73728ff814b64eb3412"),
        "name" : "Whiskers",
        "age" : 4,
        "breed" : "Scottish Fold",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : true
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : Timestamp(1635457867, 2)
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602eb84628ff814b64eb3413"),
        "name" : "Krissy",
        "age" : 6,
        "breed" : "Turkish Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : Timestamp(1635457867, 3)
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebc5ea78db75e09057cd0"),
        "name" : "Cassy",
        "age" : 16,
        "breed" : "Japanese Angora",
        "personality" : {
                "dogFriendly" : true,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : Timestamp(1635457867, 4)
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebc9da78db75e09057cd1"),
        "name" : "Shane",
        "age" : 11,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : Timestamp(1635457867, 5)
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}
{
        "_id" : ObjectId("602ebed7a78db75e09057cd2"),
        "name" : "Phoebe",
        "age" : 21,
        "breed" : "Bobtail",
        "personality" : {
                "dogFriendly" : false,
                "childFriendly" : false
        },
        "moreDetails" : {
                "cost" : 30000,
                "adoptionDate" : Timestamp(1635457867, 6)
        },
        "likes" : [
                "fish",
                "milk",
                "chicken",
                "blankets"
        ]
}

Conclusion

In this article, we learned to use the $currentDate operator in MongoDB. Learn more about MongoDB and NodeJS on CodeForGeeks.com

Noteworthy References

https://docs.mongodb.com/manual/reference/operator/update/currentDate/