Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)
I want to know the difference between Date.now() and Date.now in mongoose... As I am a little bit confused between the two. Can someone guide me on this?

1 Answer

0 votes
by (108k points)

Date.now() is considered to be an inbuild function of JavaScript. If you pass the date.now() in mongoose then this means that you are basically passing the returned value of Date.now() function. Whereas the data.now will return the function itself. With the following schema definition, Mongoose will populate the generated date 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}

});

The following is the JavaScript code against your schema:

yourSchema.pre('save', function doSomething(next){

   var something = this;

   something.createdAt(Date.now());

   next();

 });

If you are a beginner and want to know more about MongoDB architecture, then do check out the link. 

Related questions

Browse Categories

...