Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of setting TTL Index to automatically clear data and expired data by Mongodb

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Mongodb is usually used to store cached data or large-size, low-value data. For these types of data, the amount of data is often very large. If it is not cleaned regularly, it will not only affect performance, but also waste a lot of system resources.

Mongodb can actually set the expiration time of data like Redis. TTL index is a special index in MongoDB, which can support automatic expiration and deletion of documents after a certain period of time. Currently, TTL indexes can only be established on a single field.

When you build a TTL index in a field in the collection, there will be a single thread in the background to determine whether the document has expired by constantly querying the value of the index (default 60s once), and the action to delete the document is also based on the load of the mongod instance. If the load is high, it may be slightly delayed for a period of time.

Create TTL index method:

The method of creating an index is the same as that of a normal index, except that an additional attribute is added

Example: in the collection of log_events, create an TTL index that expires one hour later on the createTime field.

Db.log_events.createIndex ({"createTime": 1},-field name {expireAfterSeconds: 60cm 60})-Expiration time (in seconds)

In the above example, the creteTime field type must be of type Date ()

Modify the value of the expireAfterSeconds attribute of the TTL index:

Note: if you want to change the expiration time expireAfterSeconds, you can use the collMod method, otherwise you can only use the dropIndex (), createIndex () method to rebuild the index, but this method is a headache in the hundreds of millions of data.

Db.runCommand ({collMod: "log_events",-collection name index: {keyPattern: {createTime: 1},-createTime is the field name with TTL index expireAfterSeconds: 7200-modified expiration time (seconds)})

Although the above method can achieve automatic expiration deletion, if the business is very busy during the day, frequent deletion of data is bound to increase the load, so I want to delete expired data regularly at night (if there is less business at night).

The methods are as follows:

Add an expireTime field (to specify the expiration time) and set the value of the expireAfterSeconds attribute to 0

Note: the above createTime field does not need to have a TTL index, and the time of this expireTime needs to be specified at the time of insertion.

Db.log_events.createIndex ({"expireTime": 1},-- field name {expireAfterSeconds: 0})-Expiration time (in seconds) db.log_events.insert ({"expireTime": new Date ('Jan 22, 2019 2300 db.log_events.insert),-specify automatic deletion time when inserting documents "logEvent": 2, "logMessage": "Success!"})

Using spring-data-mongodb 2.0.9

Document document = new Document (); document.append ("createTime", 1); IndexOptions indexOptions = new IndexOptions (); indexOptions.expireAfter (300L TimeUnit. Second); this.mongoTemplate.getCollection ("test") .createIndex (document,indexOptions)

CreateTime is a time field

Using spring-data-mongodb 1.7.0

BasicDBObject bson = new BasicDBObject (); bson.append ("createTime", 1); BasicDBObject options = new BasicDBObject (); options.append ("expireAfterSeconds", 7200); this.mongoTemplate.getCollection ("test") .createIndex (bson,options)

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support. If you want to know more about it, please see the relevant links below.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report