Amey Jah
Amey Jah

Reputation: 913

mongodb - does expire data ttl settings actually index and deletes source collection when all docs expire

I have a question about expire TTL settings on documents.

My use case:

My application is kind of aggregator service. It creates one new collection per source. Sources may come and go (read die). My data access pattern is less number of reads and more number of writes. While some of core collections (which do not change) are indexed, collections that application creates per source are not indexed. Generally whole data or partial subset of data (like just 10 records) from that collection is fetched so no need to index really.

Problem:

Those dynamic collections from sources are getting bigger and after certain period of time, we don't need older documents from these collections. So I was thinking of applying "expire TTL settings". However, I had few questions around it.

Questions:

  1. Does it actually create index which helps in searching. I mean I do not want those documents to be indexed. Currently, I am just fetching all from the collection. That is our use case.
  2. Does it also deletes the collection when all documents in that collection are expired. this might be inner details but I thinking having a clue about this would be helpful

Upvotes: 1

Views: 1592

Answers (1)

John Petrone
John Petrone

Reputation: 27487

Expire TTL settings on a document are implemented and enforced thru an index on a BSON date object with the expireAfterSeconds flag set. So you have to have an index on that field in order to make TTL work. Not certain why you have an issue about indexing the documents but this is the only way that TTL works.

Regarding does the index created help in searching - yes it does. You can test this by creating an index on a date object using expireAfterSeconds and then run a a query on that field and use explain() - you will see that it does use the index for a search.

Regarding deleting the collection no it does not do that - it just deletes documents that are expired.

Upvotes: 3

Related Questions