Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When renaming a MongoDB collection, are the indices intact?

Tags:

mongodb

When performing this operation, will indices remain intact?

db.collection('my-collection').rename('new-collection-name', {dropTarget:true});
like image 857
dani Avatar asked Oct 28 '25 10:10

dani


1 Answers

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.

like image 106
Andrew Newdigate Avatar answered Oct 30 '25 00:10

Andrew Newdigate