Does indexes are updated when documents are deleted from the collection?

I read this text “Delete operations do not drop indexes, even if deleting all documents from a collection.” in the manual. ( Delete Documents — MongoDB Manual )

Does this mean that the index size does not change if some documents are removed by TTL or delete operations ?

Does this behavior also apply to TTL of the time-series collection ?

Hi @bgkim welcome to the community!

The sentence in question is saying that index will not be dropped even if you delete all documents in the collection. However the index will be dropped if the collection is dropped. This is important because you might want to clean up your collection, but you want to keep your index definition intact.

To answer your question, an index entry is a pointer toward a document’s physical location. Thus if the document is removed, the index entry that pointed toward that document is also removed (via TTL or otherwise). If you don’t, you’ll have a disconnect between the content of the index and the collection, which is not a good situation to be in.

Yes, although the actual details are different. Time series is a special kind of collection, and it handles index differently compared to a regular collection. The same idea applies here as well though: if you delete a document, the corresponding index entry that points to that document will also be deleted.

Best regards
Kevin

1 Like