Constant value for default date in mongo atlas

My tech stack is Nest JS with mongo DB atlas.
When I create a document, I want a field ‘dateAdded’ parameter to be set with the value of current time by default. And for that I am not passing in the time parameter but defined it to be a default value with ‘new Date()’ in schema.
In Atlas this operation is always creating a constant time string to be created. Irrespective of when the document is created.
How to solve this ?

This is the schema for my conversations collections

@Schema({ collection: 'conversations', discriminatorKey: 'conversationType' })
export class Conversation {
    @Prop({
        type: String,
        required: true,
        enum: [GhlConversation.name, PodiumConversation.name],
    })
    conversationType: string;

    @Prop({
        type: String,
        required: true,
        default: new Date().toISOString(),
    })
    dateAdded: string;

    @Prop({ required: false })
    messageType?: string;

    @Prop({ required: true })
    direction: 'inbound' | 'outbound';
}

Hey @Prasanjit_Dutta,

Welcome to the MongoDB Community!

I tried it in my environment and it worked perfectly using the schema you provided. Could you please specify the version of MongoDB you’re using and share some sample documents that have been created? Additionally, could you confirm whether it worked in a local MongoDB deployment or not?

May I ask about the choice of using a string data type for dateAdded instead of a date? Is there a specific reason behind this?

Looking forward to your response.

Best regards,
Kushagra

I double that:

I hope the reasons are good because dates as string are 21 bytes bigger than dates as Date.

Dates as string are slower to compare because it is done character per character.

Dates as date provide a much richer API.