A strange question about mongodb index The data index actually written has not been completed so the query cannot be completed?

  mongodb, question

For example, the script is constantly writing data (the level of data is very deep), the tail -f log can see that 10,000 pieces of data have been written, and the data can also be found in mongodb, but direct count has only 9,800 pieces of data, and has been lagging behind 200 pieces, and about 9 indexes related to this data (don’t ask me why there are so many indexes, and I don’t want to either. I can’t decide either) if the index’s attribute background is true, can we assume that when the index is used, if this data index is not established, it cannot be queried?

Need to look at the implementation plan:

Db. < table >. explain(true).count (< condition >);

From this we can know whether the index can be used and which index to use when count.
{background: true}The index is only created for the first time without blocking the foreground thread, not asynchronously, so there is no problem as you said.
It is more likely that one or more of the nine indexes arePartial indexAnd the execution plan selects this index. You can view the index configuration:

Db. < table >. getIndexes ();

Usually MongoDB is unlikely to choose such an index to count, so it may also be a specific version of the bug, hoping to see your server version to confirm:


To sum up, we need to see:

  • Implementation plan
  • Configuration of 9 Indexes
  • MongoDB version