{"id":6026,"date":"2012-07-04T11:19:55","date_gmt":"2012-07-04T05:49:55","guid":{"rendered":"https:\/\/2thenew.today\/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:\/\/2thenew.today\/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=\"https:\/\/2thenew.today\/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":45,"footnotes":""},"categories":[1],"tags":[55,260,4846,453],"class_list":["post-6026","post","type-post","status-publish","format-standard","hentry","category-technology","tag-javascript","tag-linux","tag-mongodb","tag-shell"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"In this post, we will learn how to use javascript as MongoDB shell script to export (fields of) a collection. If you just want to execute MongoDB commands then look at the another post there.. OK, so let&#039;s focus back to the objective of this post. Generally, to export data from a MongoDB\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Salil\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Export very large data from a MongoDB collection using script | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"In this post, we will learn how to use javascript as MongoDB shell script to export (fields of) a collection. If you just want to execute MongoDB commands then look at the another post there.. OK, so let&#039;s focus back to the objective of this post. Generally, to export data from a MongoDB\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Export very large data from a MongoDB collection using script | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"In this post, we will learn how to use javascript as MongoDB shell script to export (fields of) a collection. If you just want to execute MongoDB commands then look at the another post there.. OK, so let&#039;s focus back to the objective of this post. Generally, to export data from a MongoDB\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/export-very-large-data-from-a-mongodb-collection-using-script\\\/#article\",\"name\":\"Export very large data from a MongoDB collection using script | TO THE NEW Blog\",\"headline\":\"Export very large data from a MongoDB collection using script\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2012-07-04T11:19:55+05:30\",\"dateModified\":\"2016-12-19T15:30:08+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":3,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/export-very-large-data-from-a-mongodb-collection-using-script\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/export-very-large-data-from-a-mongodb-collection-using-script\\\/#webpage\"},\"articleSection\":\"Technology, javascript, Linux, MongoDB, shell\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/export-very-large-data-from-a-mongodb-collection-using-script\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/export-very-large-data-from-a-mongodb-collection-using-script\\\/#listItem\",\"name\":\"Export very large data from a MongoDB collection using script\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/export-very-large-data-from-a-mongodb-collection-using-script\\\/#listItem\",\"position\":3,\"name\":\"Export very large data from a MongoDB collection using script\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/\",\"name\":\"Salil\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/export-very-large-data-from-a-mongodb-collection-using-script\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/97e216cba95b296d5909ad7b93d1c338d0fdad7b95410c9412b126a90f4735d9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Salil\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/export-very-large-data-from-a-mongodb-collection-using-script\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/export-very-large-data-from-a-mongodb-collection-using-script\\\/\",\"name\":\"Export very large data from a MongoDB collection using script | TO THE NEW Blog\",\"description\":\"In this post, we will learn how to use javascript as MongoDB shell script to export (fields of) a collection. If you just want to execute MongoDB commands then look at the another post there.. OK, so let's focus back to the objective of this post. Generally, to export data from a MongoDB\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/export-very-large-data-from-a-mongodb-collection-using-script\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"datePublished\":\"2012-07-04T11:19:55+05:30\",\"dateModified\":\"2016-12-19T15:30:08+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Export very large data from a MongoDB collection using script | TO THE NEW Blog","description":"In this post, we will learn how to use javascript as MongoDB shell script to export (fields of) a collection. If you just want to execute MongoDB commands then look at the another post there.. OK, so let's focus back to the objective of this post. Generally, to export data from a MongoDB","canonical_url":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/#article","name":"Export very large data from a MongoDB collection using script | TO THE NEW Blog","headline":"Export very large data from a MongoDB collection using script","author":{"@id":"https:\/\/2thenew.today\/blog\/author\/salil\/#author"},"publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"},"datePublished":"2012-07-04T11:19:55+05:30","dateModified":"2016-12-19T15:30:08+05:30","inLanguage":"en-US","commentCount":3,"mainEntityOfPage":{"@id":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/#webpage"},"articleSection":"Technology, javascript, Linux, MongoDB, shell"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.today\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/2thenew.today\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/#listItem","name":"Export very large data from a MongoDB collection using script"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/#listItem","position":3,"name":"Export very large data from a MongoDB collection using script","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.today\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.today\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.today\/blog\/author\/salil\/#author","url":"https:\/\/2thenew.today\/blog\/author\/salil\/","name":"Salil","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/97e216cba95b296d5909ad7b93d1c338d0fdad7b95410c9412b126a90f4735d9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Salil"}},{"@type":"WebPage","@id":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/#webpage","url":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/","name":"Export very large data from a MongoDB collection using script | TO THE NEW Blog","description":"In this post, we will learn how to use javascript as MongoDB shell script to export (fields of) a collection. If you just want to execute MongoDB commands then look at the another post there.. OK, so let's focus back to the objective of this post. Generally, to export data from a MongoDB","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.today\/blog\/author\/salil\/#author"},"creator":{"@id":"https:\/\/2thenew.today\/blog\/author\/salil\/#author"},"datePublished":"2012-07-04T11:19:55+05:30","dateModified":"2016-12-19T15:30:08+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.today\/blog\/#website","url":"https:\/\/2thenew.today\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Export very large data from a MongoDB collection using script | TO THE NEW Blog","og:description":"In this post, we will learn how to use javascript as MongoDB shell script to export (fields of) a collection. If you just want to execute MongoDB commands then look at the another post there.. OK, so let's focus back to the objective of this post. Generally, to export data from a MongoDB","og:url":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/","og:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Export very large data from a MongoDB collection using script | TO THE NEW Blog","twitter:description":"In this post, we will learn how to use javascript as MongoDB shell script to export (fields of) a collection. If you just want to execute MongoDB commands then look at the another post there.. OK, so let's focus back to the objective of this post. Generally, to export data from a MongoDB","twitter:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"6026","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 12:09:28","updated":"2024-02-29 08:22:32","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tExport very large data from a MongoDB collection using script\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.today\/blog"},{"label":"Technology","link":"https:\/\/2thenew.today\/blog\/category\/technology\/"},{"label":"Export very large data from a MongoDB collection using script","link":"https:\/\/2thenew.today\/blog\/export-very-large-data-from-a-mongodb-collection-using-script\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/6026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/comments?post=6026"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/6026\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/media?parent=6026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/categories?post=6026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/tags?post=6026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}