12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
建站问答 时间:2024-11-30 19:59:22
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
在MongoDB中,可以通过设置索引的过期时间来实现数据的过期。具体步骤如下:创建一个索引并指定过期时间字段:db.collection.createIndex({ "expireAt": 1 }, { expireAfterSecon
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
在MongoDB中,可以通过设置索引的过期时间来实现数据的过期。具体步骤如下:
db.collection.createIndex({ "expireAt": 1 }, { expireAfterSeconds: 0 })
上述代码中,collection
是你的集合名称,expireAt
是你想要设置过期时间的字段名,expireAfterSeconds
指定了过期时间(单位为秒),设置为0表示使用expireAt
字段的值作为过期时间。
expireAt
字段的值为文档的过期时间:db.collection.insertOne({ "name": "John", "age": 30, "expireAt": new Date("2022-01-01T00:00:00Z") })
上述代码中,expireAt
字段的值被设置为2022年1月1日的时间。
expireAt
字段的值小于当前时间时,该文档将被自动删除。注意:为了使过期索引生效,MongoDB服务必须启用--setParameter enableDocumentValidation=false
选项。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19