schoenbl
schoenbl

Reputation: 723

Cannot get mongodb match for date range to work inside aggregation

I'm trying to filter by date range with match.

This is what I currently have setup:

const initReport = await Report.aggregate(
                [
                    {
                        "$lookup": {
                            from: Like.collection.name,
                            localField: "like",
                            foreignField: "_id",
                            as: "like"
                        }
                    }
                    {
                        "$lookup": {
                            from: Player.collection.name,
                            localField: "player",
                            foreignField: "_id",
                            as: "player"
                        }
                    },
                    {
                        "$unwind": {
                        path: "$player",
                        preserveNullAndEmptyArrays: true
                    }
                    },
                    {
                        "$lookup": {
                            from: Author.collection.name,
                            localField: "author",
                            foreignField: "_id",
                            as: "author"
                        }
                    },
                    {
                        "$lookup": {
                            from: Team.collection.name,
                            localField: "player.team",
                            foreignField: "_id",
                            as: "player.team"
                        }
                    },
                    {
                        "$unwind": {
                        path: "$player.team",
                        preserveNullAndEmptyArrays: true
                    }
                    },
                    {
                        "$lookup": {
                            from: League.collection.name,
                            localField: "player.team.league",
                            foreignField: "_id",
                            as: "player.team.league"
                        }
                    }
                    {
                        $match: {

                            "createdAt": {  $lte: '2020-07-23T16:37:29.710Z', $gte: '2017-07-23T16:37:29.710Z' }

                        }
                    },
                    { 
                        $group : { 
                            _id : "$_id",
                            comment: { "$first": "$comment" },
                            title: { "$first": "$title" },
                            summary: { "$first": "$summary" },
                            analysis: { "$first": "$analysis" },
                            source_title: { "$first": "$source_title" },
                            source_link: { "$first": "$source_link" },
                            author: { "$first": "$author" },
                            like: { "$first": "$like" },
                            player: { "$first": "$player" },
                            createdAt: { "$first": "$createdAt" }
                        }
                    }
                ]
            )

This returns an empty array.

I feel like I'm missing obvious, but I'm not sure what it is.

I've even tried to delete all of the other code outside of the aggregation, except for the date with the date, and it still does not work.

Upvotes: 0

Views: 130

Answers (1)

It'sNotMe
It'sNotMe

Reputation: 1260

In the code provided $lte and $gte are evaluating strings rather than dates. You'll find matches if you compare date objects instead.

        $match: {
        "createdAt": { $lte: new Date('2020-07-23T16:37:29.710Z'), $gte: new Date('2017-07-23T16:37:29.710Z') }
    }

Upvotes: 1

Related Questions