重命名 MongoDB 集合时,索引是否完好无损? [英] When renaming a MongoDB collection, are the indices intact?

查看:49
本文介绍了重命名 MongoDB 集合时,索引是否完好无损?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行此操作时,索引会保持不变吗?

When performing this operation, will indices remain intact?

db.collection('my-collection').rename('new-collection-name', {dropTarget:true});

推荐答案

使用 mongo cli,很容易测试:

Using the mongo cli, it's easy to test:

$ mongo
> db.bob.ensureIndex({ name: 1 })
{
    "createdCollectionAutomatically" : true,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}

> db.bob.renameCollection('robert', { dropTarget: true })
{ "ok" : 1 }

> db.robert.getIndices()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test.robert"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test.robert"
    }
]

所以,是的,看起来索引确实保持不变.

So, yes, it looks like the indices do remain intact.

这篇关于重命名 MongoDB 集合时,索引是否完好无损?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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