python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
Are mongodb ids unique ?

5 Best Ways to Rename MongoDB Collection [Rename]

How can I Rename a collection in MongoDB?”
Definition: “Rename or changes the name of an existing collection”.

Try the Video live example of the code shown in this page using Robomongo!

{renameCollection: "Original_collection_name>", to: "<New_collection_name>", dropTarget: <true | false>}

What is “renameCollection”? It is the name of the original collection to rename.

What is to”? It is the new name of the collection.

What is “dropTarget”? If dropTarget is “true”, mongod will drop the target of renameCollection before to renaming the collection. The default value is “false”.

//The db.collection.renameCollection() is not supported on sharded collections.


db.Original_Coll_Name.renameCollection('ifAlreadyExistColl', true);

> show dbs
admin    0.000GB
demoDB   0.000GB
local    0.000GB
myNewDB  0.000GB

> use demoDB
switched to db demoDB

> show collections

> db.users.renameCollection( "Users_Test")
        "ok" : 0,
        "errmsg" : "source namespace does not exist",
        "code" : 26,
        "codeName" : "NamespaceNotFound"

> db.Users.renameCollection( "Users_Test")
{ "ok" : 1 }

> show collections

> db.Users_Test.find()
{ "_id" : ObjectId("5857c0209244b5a6e9946de4"), "Name" : "Anil", "Age" : 30 }

Using “Robomongo” Management Tool to RENAME a Collection,
Reference links,
I hope you are enjoying with this post! Please share with you friends. Thank you!!

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like Powered by Blogger.
ASK Questions