Rename collection in MongoDB

You are here : Home / MongoDB Tutorial


Contents of page >


1) Rename collection in MongoDB >


renameCollection method will rename the collection in MongoDB.


Syntax of method :
db.collectionName.renameCollection("collection_NewName")
If collection_NewName already exists then you will get error - "errmsg" : "source namespace does not exist". See second method to avoid this error.


Example >
db.myCol.renameCollection("myCol_Renamed")


mycol - The name of collection to be renamed.
myCol_Renamed - name of collection after renaming.

2) Rename collection in MongoDB - Drop the target of renameCollection (if it exists) prior to renaming the collection >


renameCollection method will rename the collection in MongoDB.


Syntax of method :
db.collectionName.renameCollection("collection_NewName", "true" )


Example >
db.myCol.renameCollection("myCol_Renamed", "true" )


Second parameter is Optional. The default value is false.


If true, mongod drops the target of renameCollection prior to renaming the collection.


Example - If myCol_Renamed already exists it will be dropped and
myCol will be renamed to myCol_Renamed.


3) Summary -


So in this mongoDB tutorial we learned how to Rename collection in MongoDB.
1) Rename collection in MongoDB >
db.myCol.renameCollection("myCol_Renamed")
mycol - The name of collection to be renamed.
myCol_Renamed - name of collection after renaming.


2) Rename collection in MongoDB - Drop the target of renameCollection (if it exists) prior to renaming the collection >
db.myCol.renameCollection("myCol_Renamed", "true" )


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.



RELATED LINKS>


Using like statement (as in sql) in MongoDB

Update documents in collection in mongoDB



Delete documents in collection (table) in mongoDB

How to Delete all documents from collection in MongoDB


Labels: MongoDB
eEdit
Must read for you :