Why does a query on a sparse index perform much faster than a query on a partial index in MongoDB? I created a sparse index in my MongoDB Atlas like so:

db.detection_events.createIndex({"emp_id": 1}, {sparse: true})

When I performed this operation:

db.detection_events.count({"emp_id": {$exists: true}})

I got the result extremely quickly. Upon readon the docs for sparse indeces, MongoDB recommends to use partial indeces instead. I created a partial index like so:

db.detection_events.createIndex({"emp_id": 1}, {partialFilterExpression: {emp_id: true}})

And the same query takes a long time to execute. I got the partial index code from here.

Why does the partial index have a much higher query time than the sparse one? Is it because I created the index incorrectly?
Turns out I created the partial index incorrectly.

If you want to speed up an $exists query, you need to add the partialFilterExpression option like so:

db.detection_events.createIndex({"thingToQueryOn": 1}, {partialFilterExpression: {thingToQueryOn: {$exists: true}}})

Note the lack of quotation marks in the second document