gpt4 book ai didi

node.js - 如果 MongoDB 中已存在索引,则忽略 Mongoose 模式中的过期设置

转载 作者:太空宇宙 更新时间:2023-11-04 00:09:52 25 4
gpt4 key购买 nike

我正在 mongoose 中使用 expires 字段schema 以设置给定字段的过期时间。例如:

var Crumbs = new Schema({
...
timestamp: {
type: Date,
expires: 3600,
required: true
},
...
});

效果是预期的,并且在 MongoDB 中创建以下索引如果该索引先前不存在:

> db.crumbs.getIndexes()
[
...
{
"v" : 2,
"key" : {
"timestamp" : 1
},
"name" : "timestamp_1",
"ns" : "mydb.crumbs",
"expireAfterSeconds" : 36000,
"background" : true
}
]

但是,如果该索引以前存在,则不会创建该索引。例如,如果我在代码中更改为 expires: 360 并重新运行我的程序,则 MongoDB 中的 "expireAfterSeconds" 保持为 36000 .

有没有办法强制索引使用架构中定义的值,即使索引已经存在?

最佳答案

您无法通过架构定义来完成此操作。

最好的方法是直接在数据库中删除索引。

或者,将其从脚本中删除。例如

mongoose.connections[0].collections("collectionname")
.dropIndex("propertyName",callback)

关于node.js - 如果 MongoDB 中已存在索引,则忽略 Mongoose 模式中的过期设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50353786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com