Mongoose.prototype.Date
, or simply Mongoose Date
, is a Date
SchemaType
.
A SchemaType
is the configuration for any property of a model. See the example below:
const personSchema = new Schema({
name: String,
age: Number,
dateOfBirth: Date
})
In the example above, String
, Number
, and Date
are all SchemaTypes
. This means that they configure the fields of any document stored in our database.
The Date
SchemaType
configures MongoDB to store the date as a Date
object in our database. An example is shown below:
ISODate("1990-01-01T23:00:00Z")
The example above shows how Date
is stored in a MongoDB database.
The ISO format is the International Organization for Standardization (ISO) date format and is a standard way to express a numeric calendar date in a simple way.
In the following example, we are going to create a schema with the Date
SchemaType
and test to see if it is actually a Date
SchemaType
with the schema.path()
method.
// import mongooseconst mongoose = require("mongoose")// create a schema constructor objectconst Schema = mongoose.Schema// create a person schema and// configure paths with SchemaType// such as Dateconst personSchema = new Schema({name: String,age: Number,dateOfBirth: Date,})// test path to see if it is// actually a `Date` SchemaTypeschema.path("dateOfBirth") instanceof Date // Returns true
In the example above, true
is returned because dateOfBirth
is a truly configured path for Date
SchemaType
.