2

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.

HonorableTones
  • 140
  • 3
  • 11

2 Answers2

3

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.

Raman
  • 78
  • 2
  • 8
  • so I am using React as the front end (first time trying to use Node.js with it), could I put three inputs for the user, (month day year) and have it saved under the "Date" element in the mongoDB database? Appreciate the help. – HonorableTones Dec 19 '19 at 13:21
  • 1
    Yes, bind it with 3 different input fields and construct a `Date` object out of it. You can ref [this](https://stackoverflow.com/questions/40981982/javascript-create-date-from-year-month-day) to get an idea. – Raman Dec 19 '19 at 13:26
2

Birth-date format for mongoose Schema

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