'Mongoose Aggregate with $last on Original Array

I am using mongoose aggregation to return a custom query from a database, as shown below. Under the $addFields stage, I am generating some custom "stats" that can help with pagination client-side, such as how many documents in total, remaining number of documents, and page number.

I'd like to add a "cursor" field to this stage, which would be the createdAt field of the last element of the array queried. The createdAt field is guaranteed to be present.

To do this, I was thinking of using the $last operator, but can not figure out what to pass as the second argument. The documentation shows examples that have arrays as part of each document... not the original array of documents.

Thanks for any help or direction you might be able to point me toward! -- Here is my aggregation call:

For context, params is passed into the function, which contains the model to be queried.

  const documents = await params.model.aggregate([
    { $match: params.filters },
    { $sort: params.options.sort },
    {
      $facet: {
        [params.model.modelName]: [{ $limit: params.options.limit }],
        stats: [
          {
            $count: 'count',
          },
          {
            $addFields: {
              cursor: {$last: `$${params.model.modelName}.createdAt`}, // THIS IS THE FIELD I AM TRYING TO GET TO FUNCTION
              total: totalCount,
              remaining: {
                $cond: {
                  if: {
                    $gt: [{ $subtract: ['$count', params.options.limit] }, 0],
                  },
                  then: { $subtract: ['$count', params.options.limit] },
                  else: 0,
                },
              },
              page: {
                $ceil: {
                  $divide: [
                    {
                      $subtract: [
                        totalCount,
                        {
                          $cond: {
                            if: {
                              $gt: [
                                { $subtract: ['$count', params.options.limit] },
                                0,
                              ],
                            },
                            then: {
                              $subtract: ['$count', params.options.limit],
                            },
                            else: 0,
                          },
                        },
                      ],
                    },
                    params.options.limit,
                  ],
                },
              },
            },
          },
        ],
      },
    },
  ]);

Here is the example array of documents:

[
    {
      _id: new ObjectId("61cb2d94fc13ae690f000419"),
      name: 'Nicolette',
      age: 22,
      married: false,
      createdAt: 2020-12-29T20:06:41.000Z
    },
    {
      _id: new ObjectId("61cb2d93fc13ae690f0000da"),
      name: 'Ranice',
      age: 33,
      married: false,
      createdAt: 2020-02-20T08:49:46.000Z
    },
    {
      _id: new ObjectId("61cb2d93fc13ae690f0000cf"),
      name: 'Morganica',
      age: 18,
      married: true,
      createdAt: 2020-01-22T12:26:59.000Z
    },
    {
      _id: new ObjectId("61cb2d94fc13ae690f00046d"),
      name: 'Eunice',
      age: 59,
      married: true,
      createdAt: 2019-08-20T04:06:58.000Z
    }
  ]



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source