$project: {
  _id: 1,
  edited: 1,
  game: {
    gta: {   
      totalUserNumber: {
        $reduce: {
          input: "$gta.users",
          initialValue: 0,
          in: { $add: [{ $size: "$$this" }, "$$value"] },
        },
      },
      userList: "$gta.users", <----- paginating this
    },
    DOTA2: {
        totalUserNumber: {
          $reduce: {
            input: "$dota2.users",
            initialValue: 0,
            in: { $add: [{ $size: "$$this" }, "$$value"] },
          },
        },
        userList: "$dota2.users", <------ paginating this
      },
    },
   .... More Games
  },
I have this $project. I have paginated the list of games by using $facet,$sort, $skip and $limit after $project.
I am trying also trying to paginate each game's userList. I have done to get the total value in order to calculate the page number and more.
But, I am struggling to apply $sort and $limit inside the $project. So far, I have just returned the document and then paginated with the return value. However, I don't think this is very efficient and wondering if there is any way that I can paginate the field inside the $project.
Is there any way that I can apply $sort and $limit inside the $project, in order to apply pagination to the fields and return?
------ Edit ------
this is for paginating the field. Because, I am already paginating the document (game list), I could not find any way that I can paginate the field, because I could not find any way that I can apply $facet to the field.
e.g. document
[
   gta: {
     userID: ['aa', 'bb', 'cc' ......],
   },
   dota: {
     userID: ['aa', 'bb', 'cc' ......],
   }
   ....
]
I am using $facet to paginate the list of games (dota, gta, lol and more). However, I did not want to return all the userID. I had to return the entire document and then paginate the userID to replace the json doc.
Now, I can paginate the field inside the aggregate pipeline by using $function.
thanks to Mongodb sort inner array !
const _function = function (e) {
    e // <---- will return userID array. You can do what you want to do.
    return {
     
    };
  };
   game
    .collection("game")
    .aggregate([
      {},
      {
        $set: {
          "game": {
            $function: {
              body: _function,
              args: ["$userID"],
              lang: "js",
            },
          },
        },
      },
    ])
    .toArray();
By using $function multiple time, you will be able to paginate the field. I don' really know if this is faster or not tho. Plus, make sure you can use $function. I read that you can't use this if you are on the free tier at Atlas.