How to retreive all records by month/year with mongoose?

Viewed 53

I want to filter a collection to retreive X records, giving a specific month, filter the field "CreatedAt", by month.

const aggregatorOpts = [
    {
      $group: {
        _id: "$userId",
        count: { $sum: 1 }
      }
    }, {
      $addFields: {
        "month": {
          $month: '$createdAt'
        }
      }
    }, {
      $match: {
        month: 1 //want to pass the month where
      }
    }]

  const result = await Something.aggregate(aggregatorOpts).sort({ count: -1 }).exec()

The code above is what i already tried out.

Im also grouping by user with more occurrences in the collection, and sorting by that. Is it possible to filter by month as i want, in mongoose?

0 Answers
Related