How to get record based upon two range of date and time in mongodb with Aggregation?

Viewed 28

i'm trying to $match two $addFields dates (FromDate and ToDate) to get record between given date and time but i am faceing issue with $match two dates.

  const Objdata = await Events.aggregate([
                      {
                        $lookup: {
                          from: "MeetingReminderSetting",
                          let: {
                            flag: false,
                            cid: "$ClientID",
                            esd: "$EventStartDtByTimeZone",
                          },
                          pipeline: [
                            {
                              $match: {
                                $expr: {
                                  $and: [
                                    { $eq: ["$IsDeleted", "$$flag"] },
                                    { $eq: ["$ClientID", "$$cid"] },
                                  ],
                                },
                              },
                            },
                          ],
                          as: "p1",
                        },
                      },
                      {
                        $unwind: {
                          path: "$p1",
                          preserveNullAndEmptyArrays: false,
                        },
                      },
                      {
                        $addFields: {
                          FromDate: {
                            $dateAdd: {
                              startDate: new Date("2022-01-01T00:00:00.000+00:00"),
                              unit: "hour",
                              amount: 24,
                            },
                          },
                          Todate: {
                            $dateAdd: {
                              startDate: {
                                $dateAdd: {
                                  startDate: new Date("2022-01-01T00:00:00.000+00:00"),
                                  unit: "hour",
                                  amount: 24,
                                },
                              },
                              unit: "minute",
                              amount: 10,
                            },
                          },
                        },
                      },
                      //---issue facing------//
                      **{
                        $match: {
                          EventStartDtByTimeZone: {
                            $gte: "$FromDate",
                            $lte: "$Todate",
                          },
                        },
                      },**
                    //----------------//
                    ]);

it working when i give static date in $gte and $lte. but it not working when i apply date from >$addFields i used multiple cases like i change date in $dateToString and than campare but it don't work

0 Answers
Related