Timestamp with nodejs and mongoose
There is function that mongoose schema exposes for you that handles default values. These default values can be calculated ones. In this example, the right and easy way to achieve what you ask here is as follows
new Schema({ date: { type: Date, default: Date.now }})
When you save the object, you do not need to specify the "date" field anymore, mongoose will take care of it!
Mongoose Docs: http://mongoosejs.com/docs/2.7.x/docs/defaults.html (old)http://mongoosejs.com/docs/schematypes.html (current version)
Why go through all that extra work instead of defining your time field in your schema to be of type Date and use middleware to set?
var todoSchema = mongoose.Schema({ time: Date});todoSchema.pre('save', function (next) { if (!this.isNew) next(); this.time = new Date(); next();});