Cara menggunakan mongodb date to timestamp

In MongoDB, a special timestamp type is provided by BSON for the internal use and is not connected with the MongoDB’s regular data type for Data. The Timestamp for internal use is of 64 bit of value, where this 64 bit is a combination of two 32 bit values.

These Two Parts are:

First is a time_t value which is the most significant part, while the least significant part is an ordinal for operation, it is incremental. This two 32 bit together makes 64-bit value. The most significant part, which consists of time_t, is compared on all platforms, and then the ordinal value is compared.

Timestamp values are always unique in mongodb with a single mongod instance. Timestamps are also used in logging log and system files.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)

Cara menggunakan mongodb date to timestamp
Cara menggunakan mongodb date to timestamp
Cara menggunakan mongodb date to timestamp
Cara menggunakan mongodb date to timestamp

Cara menggunakan mongodb date to timestamp
Cara menggunakan mongodb date to timestamp
Cara menggunakan mongodb date to timestamp
Cara menggunakan mongodb date to timestamp

Price
View Courses

360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,471 ratings)

Syntax #1

Now that we have understood what timestamp is, the following is the standard syntax for timestamp: ts:

new Timestamp()

  • Explanation: Executing the above syntax will store the system’s current time and date within the document.
  • MongoDB shell provides a better way to retrieve date and time, on which a particular document was stored. To fetch the date-time, all we need is ObjectId of the document.

Syntax #2

ObjectId("5ec2f8f009f2b0337b4197fe").getTimestamp()

  • Explanation: Upon execution, the mongodb shell will retrieve the date-time associated with the object id passed. The return will be in the ISODate format, with the exact date and time of the operation. Refer the below screenshot for output:

Cara menggunakan mongodb date to timestamp

  • The MongoDB timestamp looks similar to Date data type, where it has the same 64-bit value. But it also has a few aspects where it differs from the Date. The MongoDB Timestamp is quite used for the internal purpose, and with every single instance of mongodb, the values generated for timestamp are unique.

How does Timestamp work in MongoDB?

Working of the timestamp in mongodb is simple, where when executed, the timestamp method will call the currentDate(), which will pick the current date and time of the system. This picked date and time will be stored in the collection, along with the other data values. And when we do not specify a date or current date or timestamp field, $currentDate is automatically set and stored in the document.

Examples to Implement MongoDB Timestamp

Now that we have understood what a Timestamp is in Mongodb and How It works along with its syntax let us implement it. Our first example is a blank record with the timestamp.

Example #1

Let’s insert an empty document with a timestamp to understand the implementation of timestamps. Our query below is an insert query, which is inserted into a sample blank document, in time test collection. Along with the empty record, we will pass a timestamp.

Syntax

db.timetest.insertOne( { ts: new Timestamp() } );

To explain, the above query will insert a document inside time test collection, this document is empty, has no key-value pairs except ts: which is our timestamp. Refer the below screenshot for a proper understanding of the insert query:

Code:

db.timetest.find().pretty()

Output:

Cara menggunakan mongodb date to timestamp

And as you can see, when we fetch the newly inserted record, all it returns is _id, which is default and the ts key, to which we didn’t pass any value, but it picked and stored the current date-time of the system.

Example #2

We have understood how to insert a blank document, and we will next attempt to insert a document with some key values, along with time stamp, using ISODate(). Following is the query and explanation:

Syntax

db.timetest.insertOne( { "City":"Pune", "Insert Date":ISODate() } );

The above query will insert a single document in time test collection, with key as City and Pune as value to it. The added key is “Insert Date”, which is a timestamp as of when the insertion operation happened. We’re simply using ISODate(), which picks the system’s current date-time and stores it. Refer the below screenshot:

Code:

db.timetest.find().pretty()

Output:

Cara menggunakan mongodb date to timestamp

As you can see, we have two documents, one was inserted earlier, which is empty but with time stamp, the second one proper document with some values. You can notice the difference between the timestamps saved.

Example #3

Our third example is the same as earlier, but now we will have our custom date and time. When we use ISODate(), it stored the current date-time of the system, but sometimes we might need to store a date that has been in the past or has yet to happen, and that is where we mention the date we intend to save, instead of autosave.

Syntax

db.timetest.insertOne( { "Message" : "This is Sample", "Insert Date" : ISODate("2020-03-02T01:11:18.965Z") } );

Our above query will insert a single document in time test collection, and here we now have a message, which is a simple key-value pair. Then we have our Insert Date, and with ISODate() we are passing our custom date and time.

Refer the below image for proper output:

Code:

db.timetest.find().pretty()

Output:

Cara menggunakan mongodb date to timestamp

As you can see, we have our document, which has an intended custom data stored. We have passed a simple message in the document with Insert Date. Here, instead of allowing the Shell to store the system’s current time and date, we chose to pass our values.

Example #4

As our final example, we have a query that will insert a document, with a message and just a date. This query will have our intended date but no time specified, which will result in storing time as “00:00:00”, unlike our third example where we specified everything, date, and time.

Syntax

db.timetest.insertOne({"Message" : "This is another example of timestamp","Insert Date" : new Date("2020-01-21") });

Code:

db.timetest.find().pretty()

Output:

Cara menggunakan mongodb date to timestamp

These are the various scenarios in which we might have to insert date time and implement the timestamp. These works best with suitable applications and timestamps are easy to fetch using the object id.

Conclusion

Timestamp in mongodb has multiple purposes. Most importantly, it stores the time and date of operation, along with logging system logs. We understood the working of timestamps in different ways with examples and different scenarios. Timestamps are useful as part of metadata, and when we don’t specify any timestamp, the system picks up the current date-time.

This is a guide to MongoDB Timestamp. Here we discuss an introduction to MongoDB Timestamp, how does it work and respective examples. You can also go through our other related articles to learn more –