Current Timestamp in required format


I’m trying to use atlas trigger.

Whenever there is a trigger, I want to add a field to document “i.e” current timestamp in required format,(MM-DD-YY seconds) seconds will be number of seconds in that particular day.

I tried using Date() and Date.Now() and tried referring the documentation as wellDate but I didn’t got the expected output.

Can you please help me out.


Do not worry about how your timestamp is stored in your data.

If you want to present it in a particular format do that at the application layer.

You may use $dateToParts to do than just before your application get the data.

You may also use the locale API supplied by your programming language of choice directly in your application. And to me it is better there than using $dateToParts. It is easier to scale data formatting (and user customization) at the application layer versus data access layer.

@steevej Thank you for your reply.

I want to add a field , business name concatenated timestamp (with above mentioned format) on insertion trigger.It’s a business use case where I need to represent the field in the expected format.

So I need to add field on an insertion itself in to collection.

Thus. $dateToParts
works on triggers?

Thank you for your time.

Thanks ,

As already mentioned:

@Tyler_Kaye, also confirms it is better to store timestamp in the native format and to apply the business use-case formatting at the presentation layer in your other thread Timestamp in require format - #4 by Tyler_Kaye

As he wrote:

just store the field as a Date which is supported in MongoDB and is most often the better thing to do when storing a date in the database. Your client-side application can be in charge of how it wants to convert a date into a string