Home > Net >  How to query users that have birthday today with Mongoose
How to query users that have birthday today with Mongoose

Time:07-22

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,
...
}

CodePudding user response:

You can use the date of the day as a query filter to match which users have birthdays today, for example, as follows.

const UserModel = mongoose.model('User', userSchema);
UserModel({ birthdate: 'today's date' }, function (err, result) {});

CodePudding user response:

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.

User.aggregate([
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        {
                                            $eq: [
                                                { $dayOfMonth: `$${field}` },
                                                {
                                                    $dayOfMonth: new Date(
                                                        moment().tz(
                                                            tenant.config
                                                                .timezone
                                                        )
                                                    ),
                                                },
                                            ],
                                        },
                                        {
                                            $eq: [
                                                { $month: `$${field}` },
                                                {
                                                    $month: new Date(
                                                        moment().tz(
                                                            tenant.config
                                                                .timezone
                                                        )
                                                    ),
                                                },
                                            ],
                                        },
                                        {
                                            tenantId: `User-${tenant._id.toString()}`,
                                        },
                                    ],
                                },
                            },
                        },
                    ])
                    .then((result) =>
                        result.map((r) => {
                            users.push({ tenant, user: r })
                            console.log(
                                'tenant: '   tenant._id   ', user: '   r?.name
                            )
                        })
                    )
  • Related