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

Detailed explanation of examples of MongoDB TTL Index

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Detailed explanation of examples of MongoDB TTL Index

TTL index is a special type of single-field index, which is mainly used to delete the corresponding document automatically when a specific time is satisfied. In other words, the documents in the collection have a certain period of validity, and documents that exceed the validity period will become invalid and will be removed. That is, the data will be out of date. Expired data does not need to be retained, which applies to machine-generated event data, logs, session information, and so on. This article mainly describes the use of TTL indexes.

I. TTL index

Creation method db.collection.createIndex (keys, options) options: expireAfterSeconds specifies how many seconds or array creation example db.eventlog.createIndex ({"lastModifiedDate": 1}, {expireAfterSeconds: 3600}) expires when the specified time is reached, that is, after the value of the index field plus a specific number of seconds, if the index field is an array That is, there are multiple date values on the index field, when MongoDB takes the minimum value plus expiration time (lowest ()) for non-date fields or index fields that do not contain an array of dates, the document will not fail. For documents that do not contain index fields, a background thread of the mongod operation will read the value of the index and remove the invalid documents from the collection when the TTL thread is activated. You can observe when the delete operation is deleted from db.currentOp () or from profile. When the index is created based on the background mode, the TTL thread can start to delete the invalid document during index creation. When the index is created based on the foreground mode, the TTL thread starts to delete the invalid document after the index is created. The deletion of the TTL index does not fully guarantee that it will be deleted after the expiration period. There is a delay (depending on the workload of mongod) the TTL delete document background thread removes the invalid document every 60s (so there may be cases where the expiration date is over and the document is still there) in the replica set environment, the TTL background thread works only on the primary replica, while the secondary copy is implemented by replication operations when using TTL index queries Like using non-TTL indexes, there are some restrictions that cannot create TTL indexes based on fields that already exist and TTL indexes based on non-date fields. Documents will not fail. TTL indexes do not support composite indexes based on multiple fields. Fixed-length sets are not supported.

2. Example of TTL index

# mongo-- shell localhost:27000 TTLData.js MongoDB shell version: 3.2.11connecting to: localhost:27000/testrepSetTest:PRIMARY > addTTLTestData () / / add collection data Create three records in database each with a create time that is 1 minute apartCreated three test documents, oldest being 4 mins oldNow create a TTL index with expiry of 5 mins on the createDate field as followsdb.ttlTest.ensureIndex ({createDate:1}) {expireAfterSeconds:300}) repSetTest:PRIMARY > db.ttlTest.find () / / currently inserts three documents {"_ id": 1, "createDate": ISODate ("2017-03-10T03:23:01.169Z")} {"_ id": 2, "createDate": ISODate ("2017-03-10T03:24:01.169Z")} {"_ id": 3 "createDate": ISODate ("2017-03-10T03:25:01.169Z")} / / add an index to the document on the test collection below That is, after 5 minutes, the index fails repSetTest:PRIMARY > db.ttlTest.createIndex ({createDate:1}, {expireAfterSeconds:300}) {"createdCollectionAutomatically": false, "numIndexesBefore": 1, / / Author: Leshami "numIndexesAfter": 2, / / Blog: http://blog.csdn.net/leshami "ok": 1} / / find document repSetTest:PRIMARY > db.ttlTest.find () {"_ id": 1 "createDate": ISODate ("2017-03-10T03:23:01.169Z")} {"_ id": 2, "createDate": ISODate ("2017-03-10T03:24:01.169Z")} {"_ id": 3, "createDate": ISODate ("2017-03-10T03:25:01.169Z")} / / when the specified time expires The document is deleted as follows. No document repSetTest:PRIMARY > db.ttlTest.find () can be found.

If you have any questions, please leave a message or go to the community to exchange and discuss, thank you for reading, hope to help you, thank you for your support!

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

Wechat

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

12
Report