Haris
Haris

Reputation: 368

MongoDB Aggregate field between two dates

I want to aggregate complaints based on status between two dates.

Sample Data :

enter image description here

In JSON format

Code :

const result = await Complaint.aggregate([
            {
                $match: {
                    createdAt: {
                        $gte: "2021-08-31T18:30:00.000Z",
                        $lt: "2021-09-30T18:29:59.999Z",
                    },
                },
            },
            { $group: { _id: "$status", count: { $sum: 1 } } },
        ]);

Expected result :

[ { _id: 'progress', count: 1 }, { _id: 'raised', count: 4 } ]

but result is always coming as empty []

Upvotes: 0

Views: 1759

Answers (1)

mohammad Naimi
mohammad Naimi

Reputation: 2359

try it

const result = await Complaint.aggregate([
            {
                $match: {
                    createdAt: {
                        $gte: ISODate("2021-08-31T18:30:00.000Z"),
                        $lt: ISODate("2021-09-30T18:29:59.999Z"),
                    },
                },
            },
            { $group: { _id: "$status", count: { $sum: 1 } } },
        ]);

or

const result = await Complaint.aggregate([
            {
                $match: {
                    createdAt: {
                        $gte: new Date("2021-08-31T18:30:00.000Z"),
                        $lt: new Date("2021-09-30T18:29:59.999Z"),
                    },
                },
            },
            { $group: { _id: "$status", count: { $sum: 1 } } },
        ]);

Upvotes: 2

Related Questions