Get the App
SLTechnology News&Howtos  ›  Database  › 

How to implement out-of-date Index in MongoDB

Shulou Source: shulou.com Published: 2022-05-31 21:13:41 09月22日 Update

This article introduces you how to achieve out-of-date index in MongoDB, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Example: set the expiration index and expire in 10 seconds

> db.phones.createIndex ({"time": 1}, {expireAfterSeconds:10})

{

"createdCollectionAutomatically": true

"numIndexesBefore": 1

"numIndexesAfter": 2

"ok": 1

}

Then insert the data:

Db.phones.insert ({"tel": 110,110 "code", "time": new Date ()})

Db.phones.insert ({"tel": 111l, "code": 111l, "time": new Date ()})

Db.phones.insert ({"tel": 112, "code": 112, "time": new Date ()})

Db.phones.insert ({"tel": 113, "code": 113, "time": new Date ()})

Db.phones.insert ({"tel": 114,114 "code", "time": new Date ()})

View:

> db.phones.find ()

{"_ id": ObjectId ("5992b97f0184ff511bf02bb2"), "tel": 110,110 "code", "time": ISODate ("2017-08-15T09:06:07.042Z")}

{"_ id": ObjectId ("5992b97f0184ff511bf02bb3"), "tel": 111,111,111 "code", "time": ISODate ("2017-08-15T09:06:07.046Z")}

{"_ id": ObjectId ("5992b97f0184ff511bf02bb4"), "tel": 112, "code": 112, "time": ISODate ("2017-08-15T09:06:07.084Z")}

{"_ id": ObjectId ("5992b97f0184ff511bf02bb5"), "tel": 113,113 "code", "time": ISODate ("2017-08-15T09:06:07.133Z")}

{"_ id": ObjectId ("5992b9800184ff511bf02bb6"), "tel": 114,114 "code", "time": ISODate ("2017-08-15T09:06:08.142Z")}

>

After 10 seconds (which will never be that accurate), the data stored in the lock will disappear. This feature is very helpful when saving some temporary data. Without this feature, it is very troublesome to use a relational database.

On how to achieve expired index in MongoDB to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Data index help content more features good interest guys buddies databases articles times knowledge articles examples troubles references Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info MariaDB OPPO Reno Apple