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

How to implement out-of-date Index in MongoDB

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

Share

Shulou(Shulou.com)05/31 Report--

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.

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