指定时间后自动从mongodb中删除文档 [英] Auto deletion of documents from mongodb after specified time

查看:60
本文介绍了指定时间后自动从mongodb中删除文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据 ttl 从 mongodb 的 collection 中自动删除文档.我已经浏览了其他答案并找到了以下方法:

I want to auto delete documents from a collection in mongodb based on a ttl. I have gone through other answers and figured out the following way:

db.collection.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )

这将从 createdAt 字段中删除 expireAfterSeconds 间隔后的文档.

This will delete the documents after expireAfterSeconds interval from createdAt field.

然而,上面的问题在于它不是特定于文档的.在上述场景中,所有文档将在创建后 3600 时间后被删除.但是,就我而言,集合中的每个文档都需要以不同的时间间隔删除.所以,每个文档都需要有自己的ttl.我怎样才能做到这一点?

However, the problem with above is that it is not document specific. In the above scenario, all documents will be deleted after 3600 time from creation. However, in my case, each document in the collection needs to be deleted at different time intervals. So, each document need to have its own ttl. How can I achieve this?

推荐答案

您可以为每个文档设置特定的删除时间.请检查文档 https://docs.mongodb.com/manual/tutorial/expire-data/ 在在特定时钟时间使文档过期"捕获器下

You can set specific time for each document for remove. Please check documentation https://docs.mongodb.com/manual/tutorial/expire-data/ under ‘Expire Documents at a Specific Clock Time’ capter

这篇关于指定时间后自动从mongodb中删除文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆