简体   繁体   中英

How to query users that have birthday today with Mongoose

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

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) {});

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
                            )
                        })
                    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM