MongoDB Rename Collection

By Yashwant Chavan, Views 3777, Last updated on 30-Oct-2016

In this tutorials we will learn how to Rename mongoDB Collection using db.collection.renameCollection(target, dropTarget) method.

tags mongodb

db.collection.renameCollection() syntax

db.collection.renameCollection(target, dropTarget)

target - string type parameter, New collection name.

dropTarget - Optional boolean type parameter, If true, mongod drops the target of renameCollection prior to renaming the collection. The default value is false.

rename mongoDB collection example

In below example, we will rename the articles collection to articles_bkp,

> use technicalkeeda
switched to db technicalkeeda

> show collections

> db.articles.renameCollection("articles_bkp");
{ "ok" : 1 }

> show collections

rename collection limitations

1] db.collection.renameCollection() - it cannot move a collection between databases.

2] db.collection.renameCollection() - is not supported on sharded collections.

“The db.collection.renameCollection() method and renameCollection command will invalidate open cursors which interrupts queries that are currently returning data.”



Hi there! I am founder of and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][]