skip to Main Content

I have this mongoose model

const reportUsSchema = new mongoose.Schema({
  texts: [
    {
      text: { type: String, default: "" },
      date_time: { type: Date, default: new Date() },
    },
  ],
});

I want this:

Whenever a new text is pushed in the texts field I want to insert the current datetime.

What I am getting (Problem):

I am getting the same date time when I restarted the server last time. For example if I have restarted the server 2 days ago I am getting the 2 days before date, which is wrong.

What I have tried:

  1. Using moment.js: I have tried multiple combinations of moment.js too. Adding them here:
date_time: { type: Date, default: moment() }

And

date_time: { type: Date, default: moment().format() }

And

date_time: { type: Date, default: moment().utc(true) }

And

date_time: { type: Date, default: moment().utc(true).format() }
  1. Using the built-in Date()

I am using it currently in the above code but none of above worked for me.

Note: The thing that worked is instead of depending upon this default value from the mongoose model, I have passed the current datetime value alongwith the text. And this is working well.

I could not understand the behavior. Help me understand where I was wrong.

Mongoose version: 5.10.7

2

Answers


  1. The default: new Date() option in the schema means that the date_time field will have a default value of the current date and time when the document is created or when the field is not specified.

    You can use the timestamps option:

    let ItemSchema = new Schema({
      texts: [
        {
          text: { type: String, default: "" },
        },
      ],
    },
    {
      timestamps: true
    });
    

    You can also specify the timestamp fileds:

    timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }
    
    Login or Signup to reply.
  2. Based on your comment from this answer you need to update your schema to use Date.now instead of new Date() like this:

    const reportUsSchema = new mongoose.Schema({
      texts: [
        {
          text: { type: String, default: "" },
          date_time: { type: Date, default: Date.now }, //< This change
        },
      ],
    });
    

    This is because when you use default: new Date() you are telling the schema to use a default date at the time the schema was defined, not the document. So if you deploy your app to a server and then start up your app the default value for that field in that schema will be the time your app started.

    However, if you use Date.now then mongoose knows to set the default to use the datetime at the time of document creation.

    In the mongoose docs they do recommend this: updated: { type: Date, default: Date.now }, in their kitchen sink example. They also list it here in their Default Functions example.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search