Count on $exists, compound index not applied


I am hitting a problematic performance issue for a count operation.
I am using Mongoose, and running 2 operations:

          const totalResults = await responses.countDocuments({
            knowledgeScore: { $exists: true },

          const scoredAboveCount = await responses.countDocuments({
            knowledgeScore: { $gt: knowledgeScore },

Where responses is a Mongoose collection.

I have a compound index { surveySlug: 1, knowledgeScore: 1 }

The “$exists” one leads to an alert. The indexing on “surveySlug” seems to work, as 50k document are parsed, which correspond to what I expect. But the indexing on “knowledgeScore” existence doesn’t, I should have roughly a fraction of that number of documents instead.

Weirdly enough, the “$gt” query doesn’t raise an alert, so the index seems correct.

My questions:

  • is that expected? From a few read on the web, I feel like indexing on $exists is supported. But maybe not in aggregations?
  • how can I debug that, in the sense, what should I expect from an “explain” call on this query?

Issue on our GitHub repo: Mongo aggregation on knowledgeScore not respecting index · Issue #172 · Devographics/Monorepo · GitHub