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 timing indexing example in MongoDB

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

Share

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

There is an index in MongoDB called the TTL index (time-to-live index, an index with a lifecycle) that allows you to set a timeout for each document. A document will be deleted when it reaches the preset aging level.

Data expiration is useful for certain types of information, such as machine-generated event data, logs, and session information, which only needs to be saved in the database for a limited time.

You can create an TTL index by specifying the expireAfterSeconds option in createIndex:

/ / the timeout is 24 hours. It runs in the foreground by default, and can be set to the background mode db.user_session.createIndex ({"updated": 1}, {expireAfterSeconds:60*60*24}) via background:true.

This creates a TTL index on the updated field. If a document's updated field exists and its value is of date type, the document will be deleted when the server time is expireAfterSeconds seconds later than the document's updated field.

Db.getCollection ('user_session') .insert ({_ id: NumberInt (1), "updated": new Date (), username:'lisi'})

The UTC time used by the mongodb save time will be converted to the GMT time when the query result is obtained, so you can see that the save time differs from the computer time by 8 hours (GMT+8)

Db.getCollection ('user_session') .find ({updated: {$gt: new Date ("2019-07-12 14:00:00")}) when querying, you can use new Date () to compare time directly. The parameter passed in by new Date is GMT time.

To prevent active sessions from being deleted, you can update the value of the updated field to the current time when there is activity on the session. As long as the updated time is 24 hours from the current time. The corresponding document will be deleted.

The TTL function of MongoDB relies on background threads in mongodb that read date type values in the index and delete expired documents from the collection.

MongoDB cleans up the TTL index every minute, so you should not rely on seconds to keep the index alive. And the TTL index does not guarantee that expired data is deleted immediately when it expires. There may be a delay between the time the document expires and when MongoDB deletes the document from the database. Because the background task of deleting out-of-date documents runs every 60 seconds. Therefore, the document may remain in the collection between the expiration of the document and the running of the background task.

The source code is in github.com/mongodb/mon.

Mongodb does not support the use of createIndex to reset the expiration time, you can only use the collMod command to modify the value of expireAfterSeconds:

Db.runCommand ({collMod: "user_session", index: {name: "updated_1", expireAfterSeconds: 120}})

After the modification is successful, you will receive a message like this (the previous expiration time was one minute, now it has been modified to 2 minutes)

{"expireAfterSeconds_old": 60.0, "expireAfterSeconds_new": 120.0, "ok": 120.0}

There can be multiple TTL indexes on a given set, and you can build ttl indexes in the created and updated fields respectively, but you can't use both fields to build a compound ttl index, nor can you create a TTL index or a normal index on the same field, but it can be used to optimize sorting and queries like a "normal index".

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.

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