Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Date.now() and Date.now in mongoose?

I am using Date.now() and Date.now in mongoose model.

I am a little bit confused about the difference between them. Could you please help me?

like image 475
Krishna joshi Avatar asked Feb 19 '16 09:02

Krishna joshi


People also ask

What is timestamps true in Mongoose?

If you set timestamps: true , Mongoose will add two properties of type Date to your schema: createdAt : a date representing when this document was created. updatedAt : a date representing when this document was last updated.

What is done () in Mongoose?

It just means that your new document will have a field with "done" as key and "false" boolean as value.

What is Save () in Mongoose?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.

What is difference between create and save in Mongoose?

create() is a shortcut for saving one or more documents to the database. MyModel. create(docs) does new MyModel(doc). save() for every doc in docs.


2 Answers

I know this is an old question, but the accepted answer doesn't explain the difference properly. It explains the difference in behaviour, but not how it actually works.

In your mongoose schema, your default can be either a value of the specified type or a function that returns a value of the specified type. Date.now is a built in Javascript function that returns the current unix timestamp as a number.

If you pass Date.now as the default in your mongoose schema, you are passing the function and mongoose will call that function every time a document needs a default value for that property. This results in the current time, at the time of document creation, being stored as the value for that property.

However, if you pass Date.now() instead, you are passing the value returned by Date.now() rather than the function. By doing this, your documents will get the current time, at the time of schema creation, as the default value for that property. This means that your documents will store the time of the latest deployment, which is probably not what you want.

If Date.now was a constant "replaced by Mongoose with the current datetime when creating a new record", as suggested by the accepted answer, then the following should logically work:

validUntil: {
  type: Date,
  default: Date.now + 3*60*60*1000 // 3 hours from now
}

But Date.now is not magically replaced by mongoose, it's a function and you can't add a number to a function. However, you can call a function inside your own function and add a number to the result, as demonstrated below:

validUntil: {
  type: Date,
  default: () => Date.now() + 3*60*60*1000 // 3 hours from now
}

As it wasn't clear from the accepted answer that Date.now is a function and not a mongoose constant, I wanted to clarify that. The difference between Date.now() and Date.now is that Date.now() calls the function and returns the result, while Date.now returns the function itself.

like image 162
Erik B Avatar answered Oct 12 '22 16:10

Erik B


Date.now can be used in your Mongoose schema definition to set a default value for a field, whereas Date.now() is the JavaScript equivalent. For example, when setting a default value in your schema definition, you use Date.now. With this schema definition, Mongoose will populated createdDate with the current time.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//schema
var yourSchema= new Schema({
   text: {type: String},
   createdAt: {type: Date, default: Date.now}
});

However, when writing JavaScript code against your schema, you have to use Date.now()

yourSchema.pre('save', function doSomething(next){
   var something = this;
   something.createdAt(Date.now());
   next();
 });
like image 20
Alex Avatar answered Oct 12 '22 16:10

Alex