moment(new Date().toISOString() always returns the same date and time

Viewed 1001

I am trying to use moment(new Date().toISOString() in mongoose model as shown below

var mongoose = require('mongoose');
var uniqueValidator = require('mongoose-unique-validator');
  Schema = mongoose.Schema;


var moment = require('moment');

//  https://www.npmjs.com/package/mongoose-unique-validator
var categorySchema = new mongoose.Schema(
  {
      CatId:{ type: Number, required: true, unique: true },
      Name : { type: String, required: true, unique: true },
      Description : String,
      Is_Active:Number,
      Db_Created_TimeStamp:{type:Date,default: moment(new Date().toISOString())},
      Db_Updated_TimeStamp:{type:Date,default:moment(new Date().toISOString())},
  },{collection: "Categories"})
categorySchema.plugin(uniqueValidator);
mongoose.model('Categories',categorySchema);

when data is inserted in the mongodb always it is showing the same time

Not sure whether i am missing some thing

The idea of doing it is getting the time stamp including millisecond

{ 
    "_id" : ObjectId("5a163a14ed910b1279d7ca3f"), 
    "Is_Active" : NumberInt(1), 
    "Description" : "spo22rt", 
    "Name" : "spo22rt", 
    "CatId" : NumberInt(2), 
    "Db_Updated_TimeStamp" : ***ISODate("2017-11-23T03:01:25.349+0000"), 
    "Db_Created_TimeStamp" : ISODate("2017-11-23T03:01:25.346+0000"),*** 
    "__v" : NumberInt(0)
}
{ 
    "_id" : ObjectId("5a163a2ced910b1279d7ca40"), 
    "Is_Active" : NumberInt(1), 
    "Description" : "spossss22rt", 
    "Name" : "spo22rsst", 
    "CatId" : NumberInt(1), 
    "Db_Updated_TimeStamp" : **ISODate("2017-11-23T03:01:25.349+0000"), 
    "Db_Created_TimeStamp" : ISODate("2017-11-23T03:01:25.346+0000"), 
    "__v" : NumberInt(0)**
}

Here the first record i have inserted is having time 2017-11-23T03:01:25.349+0000

After 3 minutes i inserted the second record but still same time in db

2017-11-23T03:01:25.349+0000

I am trying this insertion at 8:40AM but i am not sure what is that time 03:01:25

I am missing something fundamentals i think almost spent a day to understand but no clue to understand what is happening please help me in understanding this

2 Answers

When the default is set to a value, the value will be set when the app starts up and it first require the schema. In this case, the default value becomes the value of new Date().toISOString() when the app starts up.

Set the default to a function, so the function runs and returns the current date whenever a new model instance is created.

  Db_Created_TimeStamp: {
    type: Date,
    default: ()=> new Date().toISOString()
  },
  Db_Updated_TimeStamp: {
    type: Date,
    default: ()=> new Date().toISOString()
  },

Mongoose does provide the fields for "free" with the timestamps option:

var thingSchema = new Schema({..}, { timestamps: { createdAt: 'created_at' } });
var Thing = mongoose.model('Thing', thingSchema);
var thing = new Thing();
thing.save(); // `created_at` & `updatedAt` will be included

or, you can just use:

default: new Date().toISOString

without call the function, this is the same as:

default: ()=> new Date().toISOString()
Related