How to remove the documents older than X days from collection considering collection is having createAt date field

Can someone explain us what are the ways from Mongo DB features i can use to delete the documents which are greater than X days from the collection periodically from the mongo collection

Hi @Venkatesh_Bingi

TTL Indexes are the feature you are looking for.

Some considerations when enabling a TTL index:

  • Creating a TTL index on a populated collection will likely start deleting documents from the collection immediately you will see extra operations and IO on the cluster.
  • A large amount of inserts will have a corresponding amount of deletes when the TTL is reached.
2 Likes

In addition to what Chris mentioned about using a TTL index, Online Archive can be useful for your use case.

Note that Online Archive Atlas moves infrequently accessed data from your Atlas cluster to a MongoDB-managed read-only Federated Database Instance on a cloud object storage older than X days. It not just deletes data from your cluster, but it moves your data older than X days (or any other rule you configure) to cloud object storage.

Here are some useful links:

4 Likes

Thanks for your suggestion @chris I would like to know does TTL Index cause any performance issue ?