Automatically delete documents upon expiration on MongoDB

I am developing a PHP application that stores a historical journal in the MongoDB collection.

After 1 hour, this data is not important and is deleted by the CRON process, which starts every hour.

The question is, is there any other solution to automatically delete these documents some time after it is inserted?

Many thanks!

+3
source share
1 answer

To expire documents after a certain number of seconds, you can create a TTL index, which is a special property of the index, which relies on the background thread in mongod, which reads the values ​​of the target date and performs delete operations for those who have expired.

createdAt, , , "new Date()"

TTL , verifyIndex() expireAfterSeconds.

db.log.ensureIndex({ "createdAt": 1}, {expireAfterSeconds: 3600})

TTL, MongoDB , createdAt , createdAt + expireAfterSeconds (3600 = 60 * 60 ).

, , 60

, : http://mongodbspain.com/en/2014/02/08/expire-data-from-collections-using-ttl/

.

+2

All Articles