Follow dba-ninja.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

dba-ninja.com Links

Dba_db2_button

Sqlserver_dba_button

How to automatically delete MongoDB documents after a certain time with TTL indexes

08 July,2021 by Rambler

Question: We have event log data going into a MongoDB collection. There is no requirement to keep this data beyond 60 days. Is there a way of automating the process to delete the rows , without building procedures to manage the data deleted.

Answer: I'd recommend you use TTL indexes for this type of problem.   The TTL (Time to Live) index is based on a single-field .    

As part of using a TTL index consider the imapct of rolling this data out of the collection. How critical is the data? 

This example creates an index on the "myeventlog" collection - changeDate field. The row will be deleted 1 hr (3600 secs) after the "changeDate"

db.myeventlog.createIndex( { "changeDate": 1 }, { expireAfterSeconds: 3600 } )

 

 

Read more on MongoDB

MongoDB Performance Tuning

How to read MongoDB system log file


Author: Rambler (http://www.dba-ninja.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to automatically delete MongoDB documents after a certain time with TTL indexes


dba-ninja.com