2

Say I have a MongoDB instance and I want my data to be automatically deleted if it expires after 90 days or 30 days.

Say my data are just stock market stock prices updated/inserted every 5 seconds.

What's the best ways/method to do that? I can use MMS but I want to have my own control, using my own code.

Community
  • 1
  • 1
Xianlin
  • 1,139
  • 5
  • 20
  • 34
  • 1
    Use [TTL index](http://docs.mongodb.org/manual/tutorial/expire-data/) – Neo-coder Jun 16 '15 at 04:40
  • that's one way of doing it. Expire data from collections. What if I want to Expire collections from the database? Say I create every collection based on every day. I have 30 collections and when I create 31st collection I will delete the 1st collection (expire 30days). – Xianlin Jun 16 '15 at 05:48
  • Do you need to query the data across multi-day periods? If so, a single collection (or collections based on the required query granularity, such as weekly) would probably be more appropriate. In any case, the only server-side expiry option is to use TTL indexes. Managing expiry by dropping old collections might be more efficient (no extra index or lookup required) as long as this approach suits your query requirements. – Stennie Jun 16 '15 at 07:48
  • I no need to query across multi-days record? how to proceed with drop old collections? Thanks – Xianlin Jun 16 '15 at 10:12
  • Expire Documents after a Specified Number of Seconds . https://docs.mongodb.com/manual/tutorial/expire-data/ ```code > db.trace_d_b.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } ) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 7, "numIndexesAfter" : 8, "ok" : 1 } ``` You can modify the expireAfterSeconds as you wanted. – Hua Zhang Oct 09 '19 at 00:09

0 Answers0