Counting the number of documents in a partial index

Hello there,

A pretty lame question, sorry about that, but I have been looking for the answer everywhere here and on the Internet and failed to find the answer.
I am trying to count the number of documents in a partial index (whole index or part of it). Not in a whole collection.
The only recommendation I found is to perform a find with the index hint or with a filter that matches the index, and count the number of documents returned. This looks like O(N), which is unreasonable. There should be a way to obtain that information as O(1) if the whole index count is requested, and at worst O(log(N)) in case counting the number of documents is operating on a sub-index.
I am using the MongoDB C driver, so the solution I need must fit within the API provided by it. I see only collection-level functions (mongoc_collection_count_documents, mongoc_collection_estimated_document_count).

Thanks in advance!

I do not know if you can get what you want exactly but the following is the closest I could find.

One thing to note is that collStats counters are reset to 0 if the mongod process is restarted, so not a reliable source.

1 Like

Ah yes, then not a solution indeed.