'How check if there exists a date in an aggregate query with Mongoose?

I am working on a location based game an have an addressmodel like this:

const AddressSchema = new Schema({
    address: String,
    city: String,
    postal_code: String,
    country: String,
    user_id: String,
    postalCodeMeters: Number,
    date: Date
});

And the following query, which extracts some data for each postal code. This query works nicely:

await AddressModel.aggregate([
                    {
                        $match: {
                            user_id: req.params.userid,
                        },
                    },
                    {
                        $group: {
                            _id: '$postal_code',
                            meters: {
                                $sum: '$postalCodeMeters',
                            },
                            count: {
                                $sum: 1,
                            },
                            address: {
                                $addToSet: '$address',
                            },
                        },
                    },
                    {
                        $project: {
                            _id: 0,
                            postal_code: '$_id',
                            count: 1,
                            meters: 1,
                            uniqueAddressesVisited: {
                                $size: '$address',
                            },
                        },
                    },
                ]);

My problem is that I am trying to add another field to return, called "isVisitedToday", which checks if the player has visited that postal code today? How would I go about doing that? I was thinking of using startOfDay and endOfday from date-fns, so I need a way of checking if there exists any date between those parameters for each postal code.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source