Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

date
mongoose
mongodb
communitycreator

What is Mongoose.prototype.Date in Mongoose?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 BSONBinary JSON native 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.

Code

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 mongoose
const mongoose = require("mongoose")
// create a schema constructor object
const Schema = mongoose.Schema
// create a person schema and
// configure paths with SchemaType
// such as Date
const personSchema = new Schema({
name: String,
age: Number,
dateOfBirth: Date,
})
// test path to see if it is
// actually a `Date` SchemaType
schema.path("dateOfBirth") instanceof Date // Returns true

In the example above, true is returned because dateOfBirth is a truly configured path for Date SchemaType.

RELATED TAGS

date
mongoose
mongodb
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring