我可以更改现有索引的名称吗?

时间:2012-10-11 18:12:05

标签: mongodb

我正在努力为MongoDB部署生成drop / create脚本。我们希望在我们的开发,测试和生产环境中找到所有MongoDB数据库/集合在结构上相同的地方。为此,我们决定在ensureIndex时间命名索引。问题是,如何更新已存在的索引名称?我所知道的不起作用就是使用" name"重新运行ensureIndex。指定像....

// existing indexes, note its name is "groups_1"...
dmReplSet:PRIMARY> db.system.indexes.find();
{ "v" : 1, "key" : { "groups" : 1 }, "ns" : "test.config", "name" : "groups_1" }
...

// attempt to change its name by re-issue ensureIndex command...
dmReplSet:PRIMARY> db.config.ensureIndex( { "groups" : 1 }, { "name" : "config_groups_ix" } );

// Nope, name is still "groups_1"...
dmReplSet:PRIMARY> db.system.indexes.find();
{ "v" : 1, "key" : { "groups" : 1 }, "ns" : "test.config", "name" : "groups_1" }
...

如何更新已存在的mongo索引的名称?这样做有风险吗?

1 个答案:

答案 0 :(得分:2)

可悲的是,从MongoDB 3.0.3开始,这是不可能的。 official documentation建议删除并重新创建索引

相关问题