Filtering multiple nested ObjectId's

That seems like a good idea. However if a color is not found in a variations array, it will still return an empty array, which defies the whole point of variations: { $ne: [] } in my filters, and I do not with for this to happen.

Moreover, I am using countDocuments(queryObj) to count how many items with a certain filter I have so I can create a pagination. Using your suggestedway kinda ruins it? Unless there is something I am missing.

Here is my function, with the way you suggested, using buildProductPopulateQuery(req.query), in case you need to take a look:

Get All Products
exports.getAll = async (req, res) => {
  try {
    const { offset, limit } = req.query;
    const offsetInt = parseInt(offset) || 0;
    const limitInt = parseInt(limit) || 15;

    const queryObj = buildProductQuery(req.query);
    const populateQuery = buildProductPopulateQuery(req.query);

    console.log(JSON.stringify(populateQuery));

    const products = await Product.find(queryObj)
      .limit(limitInt)
      .skip(offsetInt)
      .populate(populateQuery);

    const totalProductsCount = await Product.countDocuments(queryObj);
    const totalPages = Math.ceil(totalProductsCount / limitInt);
    const currentPage = Math.ceil(offsetInt / limitInt) + 1;

    return Response.success(res, {
      products,
      pagination: {
        total: totalProductsCount,
        pages: totalPages,
        current: currentPage,
      },
    });
  } catch (err) {
    return Response.serverError(res, err.message);
  }
};