Mongoose, filter array to return 30 days old items

hello guys, i have a problem using mongoose. I have a document like this:

{
   "name":"name",
   "previousTreatments":[
   {
      "name":"lorem ipsum",
      "specific":"lorem ipsum",
      "createdAt": 2022-06-05T09:22:52.339+00:00  //Mongoose Date Object
   },
   {
      "name":"lorem ipsum 2",
      "specific":"lorem ipsum 2",
      "createdAt": 2022-07-05T09:22:52.339+00:00 //Mongoose Date Object
   }
]
}

Now, i want to retrieve all “previousTreatments” items that are 30 days old. How can i do this with aggregate method? Obviously i want to check this only on the last index of this array (not all)

just the last items that is 30 days old for ALL the clients

i’ve tried something like:

my actual document:

{
   "name":"name",
   "previousTreatments":[
   {
      "name":"lorem ipsum",
      "specific":"lorem ipsum",
      "createdAt": 2022-06-05T09:22:52.339+00:00  //Mongoose Date Object
   },
   {
      "name":"lorem ipsum 2",
      "specific":"lorem ipsum 2",
      "createdAt": 2022-07-05T09:22:52.339+00:00 //Mongoose Date Object
   }
]
}
const today = new Date();
const priorDate = new Date(new Date().setDate(today.getDate() - 30));

    console.log(today, priorDate)
    /*
    * Console log result
    *   today = 2022-08-05T16:31:47.219Z 
    *  priorDate = 2022-07-05T16:31:47.219Z
    */

    try {
        const treatmentReminder = await Clienti.aggregate([
            {
                $project: {
                    previousTreatments: {$arrayElemAt: ["$previousTreatments", -1]}
                }
            },
            {
                $match: {"previousTreatments.createdAt": priorDate}
            }
        ])
        if (!treatmentReminder){
            return res.status(400).send('No reminders found')
        }
       res.status(200).send(treatmentReminder)
    } catch (err) {
        res.status(404).send('An error has occurred')
    }

but i can’t find a way to match only the yyyy-mm-dd of the date object

also my last try not work completely…it match dates that are NOT greater than “priorDateEnd”, but it show all previous days also…

is possible to match date in the 24 hours range? basically i need all the items that are 1 month old in 24 hours

my last try:

const today = new Date()
    const priorDate = new Date(new Date().setDate(today.getUTCDate() - 31))
    const priorDateStart = new Date(priorDate.setHours(0, 0, 0, 0))
    const priorDateEnd = new Date(priorDate.setHours(23, 59, 0, 0))

    console.log(priorDateStart, priorDateEnd)

    try {
        const treatmentReminder = await Clienti.aggregate([
            {
                $match: {
                    'previousTreatments.createdAt': {
                        $gt: priorDateStart,
                        $lte: priorDateEnd,
                    },
                },
            },
            {
                $project: {
                    mobilePhone: 1,
                    name: 1,
                    surname: 1,
                    previousTreatments: {
                        $arrayElemAt: ['$previousTreatments', -1],
                    },
                },
            },
        ])

i think i’ve found a solution, using momentJs startOf and endOf works perfectly fine!