1

So im, getting alert emails from mongoDB about me indexing more than a thousand values than im returning, i know these kinds of alerts arent mission critical, but that im inefficiently targeting the database, and i believe ive narrowed down my queries to where im returning a bulk majority of objects, what im doing is counting ALL documents in my database and using that as a reference for how many pages (for pagination) there are. I believe this is whats causing my scanning vs. indexing alerts. I know with mongoose the aggregation query has a $count operator, but from what i can tell, this count, will only return the number of matching documents, it wont also return the documents that matched the query. what i need to do is find a way to make these two queries one query, this should get rid of my scan vs indexing error.

but im at a loss, everything i've researched points to $count but i need the actual objects back from the database in order to proceed, and if i create another query to return those, then im essentially doing the same thing im doing now, which is what im trying to avoid.

this isnt much of an issue now, but its likely my videos table will double in size by the end of the month, and then im hoping to triple in size that value next month.

const count = await videoModel.countDocuments({
      ...sortBy,
      $or: [
        {
          videoTitle: {
            $regex: keyword,
            $options: "i",
          },
        },
      ],
    });
    const videos = await videoModel.aggregate([
      {
        $match: {
          ...sortBy,
          $or: [
            {
              videoTitle: {
                $regex: keyword,
                $options: "i",
              },
            },
          ],
        },
      },
      {
        $sort: {
          createdAt: -1,
        },
      },
      {
        $skip: pageSize * (page - 1),
      },
      {
        $limit: pageSize,
      },
    ]);
Austin Howard
  • 780
  • 4
  • 10
  • 24
  • Is [this](https://stackoverflow.com/questions/71736587/how-to-access-overall-document-count-during-arithmetic-aggregation-expression) what you are looking for? – nimrod serok Jul 06 '22 at 17:38
  • @nimrodserok, yours was closest to the right answer, i found a solution using the `$setWindowFields` operator, it essentially adds, a value `totalCount` to each document. i can use that value, to do what i need, i used the answer from https://stackoverflow.com/a/72838708/3939805 – Austin Howard Jul 06 '22 at 18:02
  • I'm happy it helped you. If you like it, vote for it – nimrod serok Jul 06 '22 at 18:15

0 Answers0