When performing this operation, will indices remain intact?
db.collection('my-collection').rename('new-collection-name', {dropTarget:true});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With