Renaming a GridFS object in mongoDB

Recently I was asked to rename a GridFS object from one of our mongoDB databases.  As of version 2.4.6 of mongoDB, this is not straight forward right click & rename command. Below in my example, I am going to rename the my_videos GridFS object (below is a view of the GridFS object in the MongoVUE application).

As viewed in MongoVUE

The GridFS object is a set of two collections.  One of the collections has an extension “.files”, which stores the metadata for the files being stores.  The other collection has an extension “.chunks”, which stores the files.   Pictured below is a screen shot of the mongo shell.  I have connected to my_database, and ran the “show collections” command.  In the example below, the GridFS object is the combination of the my_videos.chunks & my_videos.files collections.   The “_id”  field in the document of the “.files” collection is linked to the “field_id” field of the document that stores the file of the “.chunks” collection with the save value.

Show Collections

To rename the GridFS object, both the “.files” collection & “.chunks” collection need to be renamed.  Use the renameCollection command to rename both collections in the mongo shell.  Both collections need to be renamed, but the order in which they are renamed is not important.  Watch the case when using this command, the C needs to be a capital letter in the “renameCollection” or a TypeError will occur.  In my example, I am changing my_videos to training_videos.  Below I have used the syntax to change the names:

db.< collection name >.renameCollection(” < new name > “)

Rename CollectionBelow is the just a screen shot of all the command in the mongo shell to rename the my_videos GridFS object.  The last “show collections” command shows that the my_videos GridFS object has been renamed in the database.

All commands to rename GridFS object

Now in my MongoVUE application, I can see my changed GridFS object to training_videos.  I can also access them now.

MongoVUE GridFS object

 

 

Comments are closed.