{"id":461,"date":"2013-11-10T23:57:20","date_gmt":"2013-11-10T23:57:20","guid":{"rendered":"http:\/\/www.theSQLReport.com\/?p=461"},"modified":"2016-02-20T12:57:13","modified_gmt":"2016-02-20T12:57:13","slug":"renaming-a-gridfs-object-in-mongodb","status":"publish","type":"post","link":"http:\/\/www.theSQLReport.com\/?p=461","title":{"rendered":"Renaming a GridFS object in mongoDB"},"content":{"rendered":"<p>Recently I was asked to rename a GridFS object from one of our mongoDB databases.\u00a0 As of version 2.4.6 of mongoDB, this is not straight forward right click &amp; 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 <a title=\"MongoDB Desktop Application\" href=\"http:\/\/www.mongovue.com\/\" target=\"_blank\">MongoVUE<\/a> application).<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_01.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" alt=\"As viewed in MongoVUE\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_01.jpg\" width=\"649\" height=\"265\" \/><\/a><\/p>\n<p>The <a title=\"GridFS\" href=\"http:\/\/docs.mongodb.org\/manual\/core\/gridfs\/\" target=\"_blank\">GridFS object<\/a> is a set of two collections.\u00a0 One of the collections has an extension &#8220;.files&#8221;, which stores the metadata for the files being stores.\u00a0 The other collection has an extension &#8220;.chunks&#8221;, which stores the files.\u00a0\u00a0 Pictured below is a screen shot of the mongo shell.\u00a0 I have connected to my_database, and ran the &#8220;show collections&#8221; command.\u00a0 In the example below, the GridFS object is the combination of the my_videos.chunks &amp; my_videos.files collections. \u00a0 The &#8220;_id&#8221;\u00a0 field in the document of the &#8220;.files&#8221; collection is linked to the &#8220;field_id&#8221; field of the document that stores the file of the &#8220;.chunks&#8221; collection with the save value.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_02.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" alt=\"Show Collections\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_02.jpg\" width=\"779\" height=\"183\" \/><\/a><\/p>\n<p>To rename the GridFS object, both the \u201c.files\u201d collection &amp; \u201c.chunks\u201d collection need to be renamed.\u00a0 Use the <a title=\"Rename Collection\" href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/db.collection.renameCollection\/\" target=\"_blank\">renameCollection <\/a>command to rename both collections in the mongo shell.\u00a0 Both collections need to be renamed, but the order in which they are renamed is not important.\u00a0 Watch the case when using this command, the C needs to be a capital letter in the &#8220;renameCollection&#8221; or a TypeError will occur.\u00a0 In my example, I am changing my_videos to training_videos.\u00a0 Below I have used the syntax to change the names:<\/p>\n<p>db.&lt; collection name &gt;.renameCollection(&#8221; &lt; new name &gt; &#8220;)<\/p>\n<p style=\"text-align: left;\"><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename01.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-462\" alt=\"Rename Collection\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename01.jpg\" width=\"841\" height=\"151\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename01.jpg 841w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename01-300x53.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename01-210x37.jpg 210w\" sizes=\"(max-width: 841px) 100vw, 841px\" \/><\/a>Below is the just a screen shot of all the command in the mongo shell to rename the my_videos GridFS object.\u00a0 The last &#8220;show collections&#8221; command shows that the my_videos GridFS object has been renamed in the database.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename02.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-463\" alt=\"All commands to rename GridFS object\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename02.jpg\" width=\"537\" height=\"345\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename02.jpg 537w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename02-300x192.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename02-210x134.jpg 210w\" sizes=\"(max-width: 537px) 100vw, 537px\" \/><\/a><\/p>\n<p style=\"text-align: left;\">Now in my <a title=\"MongoDB Desktop Application\" href=\"http:\/\/www.mongovue.com\/\" target=\"_blank\">MongoVUE<\/a> application, I can see my changed GridFS object to training_videos.\u00a0 I can also access them now.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename03.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-466\" alt=\"MongoVUE GridFS object\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/rename03.jpg\" width=\"628\" height=\"268\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was asked to rename a GridFS object from one of our mongoDB databases.\u00a0 As of version 2.4.6 of mongoDB, this is not straight forward right click &amp; rename &hellip; <a class=\"readmore\" href=\"http:\/\/www.theSQLReport.com\/?p=461\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":466,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/461"}],"collection":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=461"}],"version-history":[{"count":5,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/461\/revisions"}],"predecessor-version":[{"id":476,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/461\/revisions\/476"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/media\/466"}],"wp:attachment":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=461"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}