I have a Users table, where I store the birth date of the user as a Date field. I want to create a query to find which users have bithdays today.
How can I accomplish that with find() function from Mongoose v5?
const userSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Please tell us your name!'],
},
email: {
type: String,
required: [true, 'Please provide your email'],
//unique: true,
lowercase: true,
validate: [validator.isEmail, 'Please provide a valid email'],
},
birthdate: Date,
...
}
2
Answers
In the end I follow the instructions from this article https://medium.com/@luansantos_4481/how-to-return-birthdays-of-the-current-day-week-and-month-with-mongodb-aggregation-f4104fe82e3c
basically is to use the aggregations pipeline on Mongoose.
You can use the date of the day as a query filter to match which users have birthdays today, for example, as follows.