是否可以避免 IndexOptionsConflict 更改 MongoDB 索引中的 expiredAfterSeconds?
Can IndexOptionsConflict be avoided to change expiredAfterSeconds in MongoDB index?
我可以用 expiredAfterSecconds
这样在 mongo shell:
中创建一个索引
db.x.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
我可以重新执行相同的命令,这对 mongo:
没问题
> db.x.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
但是,如果我尝试更改 expiredAfterSecconds
值并重新创建索引,我会收到错误消息:
> db.x.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 7200 } )
{
"ok" : 0,
"errmsg" : "Index with name: createdAt_1 already exists with different options",
"code" : 85,
"codeName" : "IndexOptionsConflict"
}
如果我使用 ensureIndex()
而不是 createIndex()
也是一样的。
据我所知,我发现修改 expireAfterSeconds
值的唯一方法是删除索引,然后重新创建它,但我想知道是否有另一种方法(也许是某种 "override" 选项传递给 createIndex()
?).
我正在使用 PyMongo,所以如果它可以解决 mongo shell 限制,那对我来说也很好。
截至 mongo doc:
- 如果某个字段已经存在非 TTL 单字段索引,则无法在同一字段上创建 TTL 索引,因为您无法创建具有相同键规范且仅选项不同的索引。要将非 TTL 单字段索引更改为 TTL 索引,您必须先删除索引并使用 expireAfterSeconds 选项重新创建。
- 您不能使用 createIndex() 更改现有索引的 expireAfterSeconds 的值。而是将 collMod 数据库命令与索引收集标志结合使用。否则,要更改现有索引的选项值,您必须先删除索引并重新创建。
关于使用 collMod
的更多信息是:here
正如@adam-harrison 在, check also
中提到的
我可以用 expiredAfterSecconds
这样在 mongo shell:
db.x.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
我可以重新执行相同的命令,这对 mongo:
没问题> db.x.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
但是,如果我尝试更改 expiredAfterSecconds
值并重新创建索引,我会收到错误消息:
> db.x.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 7200 } )
{
"ok" : 0,
"errmsg" : "Index with name: createdAt_1 already exists with different options",
"code" : 85,
"codeName" : "IndexOptionsConflict"
}
如果我使用 ensureIndex()
而不是 createIndex()
也是一样的。
据我所知,我发现修改 expireAfterSeconds
值的唯一方法是删除索引,然后重新创建它,但我想知道是否有另一种方法(也许是某种 "override" 选项传递给 createIndex()
?).
我正在使用 PyMongo,所以如果它可以解决 mongo shell 限制,那对我来说也很好。
截至 mongo doc:
- 如果某个字段已经存在非 TTL 单字段索引,则无法在同一字段上创建 TTL 索引,因为您无法创建具有相同键规范且仅选项不同的索引。要将非 TTL 单字段索引更改为 TTL 索引,您必须先删除索引并使用 expireAfterSeconds 选项重新创建。
- 您不能使用 createIndex() 更改现有索引的 expireAfterSeconds 的值。而是将 collMod 数据库命令与索引收集标志结合使用。否则,要更改现有索引的选项值,您必须先删除索引并重新创建。
关于使用 collMod
的更多信息是:here
正如@adam-harrison 在