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?
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.
It just means that your new document will have a field with "done" as key and "false" boolean as value.
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.
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.
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.
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();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With