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,
},
]);