HonorableTones
HonorableTones

Reputation: 138

Date of Birth in Mongoose Schema

I feel as though this is a simple question, but after doing google searches and looking at the mongoose documentation, I have not found a good answer (or at least one that I understand).

I want to include in the Profile Schema a Date of Birth Block. Now, this is my idea but I am unsure if this will work well within MongoDB.

I want to be able to have the User enter this information into their profile, and save it into the database.

birthday: {
day: {
  type: Number
},
month: {
  type: Number
},
year: {
  type: Number
}

Is this the best method? does it cause problems in the long run? what is your guys opinion on the proper way to using Node.Js/Mongoose Schemas and using birthdates?

I appreciate the help guys.

Upvotes: 2

Views: 9362

Answers (2)

PranaV Shimpi
PranaV Shimpi

Reputation: 15

Birth-date format for mongoose Schema

dateOfBirth: {
    type: Date,
    required: true,
    trim: true,
}

Upvotes: 2

Raman
Raman

Reputation: 88

Why not just use something like this?

birthday: { type: Date }

Whenever user inputs all the three fields, combine it to form the birthday and save it as date object. This seems much cleaner and it's easy to query too, considering mongo supports aggregation on fields like $month.

Upvotes: 4

Related Questions