{"id":6026,"date":"2012-07-04T11:19:55","date_gmt":"2012-07-04T05:49:55","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6026"},"modified":"2016-12-19T15:30:08","modified_gmt":"2016-12-19T10:00:08","slug":"export-very-large-data-from-a-mongodb-collection-using-script","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/","title":{"rendered":"Export very large data from a MongoDB collection using script"},"content":{"rendered":"<p>In this post, we will learn how to use javascript as MongoDB shell script to export (fields of) a collection.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/executing-mongodb-shell-commands-from-javascript-file\/\">If you just want to execute MongoDB commands then look at the another post there.<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>OK, so let&#8217;s focus back to the objective of this post.<br \/>\n<strong>Generally, to export data from a MongoDB collection<\/strong>, we use the following command &#8211;<br \/>\n[shell]<br \/>\nmongoexport -vvvv &#8211;host 127.0.0.1 &#8211;db my-db &#8211;username=&#8217;usr&#8217; &#8211;password=&#8217;pwd&#8217; &#8211;collection profiles &#8211;csv &#8211;out \/home\/data\/usernames.csv -f &#8216;username&#8217;<br \/>\n[\/shell]<\/p>\n<p>Definitely I prefer the above mentioned way. But let&#8217;s consider that you have millions or billions of records in a MongoDB collection. And you want to export all these.<br \/>\nProbably, this will just hang. Well, I am not sure what happens on your computer but on my machine it was just stuck for hours and then I terminated the process. And came up with scripting solution &#8211; where I created multiple files (chunks) of the exported data.<\/p>\n<p>&nbsp;<\/p>\n<p>[javascript]<br \/>\nvar size = 1000000;<br \/>\nvar maxCount = 1;<br \/>\nfor (x=0;x&lt;maxCount;x=x+1)<br \/>\n{<br \/>\n   var recToSkip = x*size;<br \/>\n   db.profiles.find().skip(recToSkip).limit(size).forEach( function(record){<br \/>\n     var username = record.username;<br \/>\n     print(record.username);<br \/>\n   })<br \/>\n}<\/p>\n<p>[\/javascript]<\/p>\n<p>And ofcourse to run the script (if above javascript is in file named &#8211; &#8216;usernames.js&#8217;)<br \/>\n[shell]<br \/>\n  mongo 127.0.0.1\/my-db &#8211;username=&#8217;username&#8217; &#8211;password=&#8217;pwd&#8217; export-usernames.js &gt; output.txt<br \/>\n[\/shell]<\/p>\n<p>The above shell command will render all the output  (from export-usernames.js) to the output.txt file. And mind it, that this is single file where you get all exported data. But if you want to chunkify this &#8211; just write your wrapper script or run multiple times. It&#8217;s upto you.<\/p>\n<p>&nbsp;<\/p>\n<p>I hope this might help someone. Or please feel free to add your comments.<\/p>\n<p>&nbsp;<\/p>\n<p>Cheers!<br \/>\nSalil Kalia<br \/>\nSalil [at] IntelliGrape [dot] com<br \/>\nTwitter LinkedIn<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will learn how to use javascript as MongoDB shell script to export (fields of) a collection. &nbsp; If you just want to execute MongoDB commands then look at the another post there.. &nbsp; OK, so let&#8217;s focus back to the objective of this post. Generally, to export data from a MongoDB [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":26},"categories":[1],"tags":[55,260,4846,453],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6026"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6026"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6026\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}