{"id":111792,"date":"2021-03-08T11:02:11","date_gmt":"2021-03-08T05:32:11","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=111792"},"modified":"2024-04-12T14:31:36","modified_gmt":"2024-04-12T09:01:36","slug":"import-and-export-mongodb-data","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/","title":{"rendered":"Import and Export MongoDB Data"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong>Import and Export MongoDB Data<\/strong><\/h4>\n\n\n\n<p>The export and import tools are useful when you want to backup or export a portion of your data without capturing the state of the entire database, or for simple data ingestion cases. For more complex data migration tasks, you may want to write your own import and export scripts using a client driver to interact with the database itself. For disaster recovery protection and routine database backup operation, use full database instance backups. Because these tools primarily operate by interacting with a running mongod instance, they can impact the performance of your running database.<\/p>\n\n\n\n<p>Not only do these processes create traffic for a running database instance, they also force the database to read all data through memory. When MongoDB reads infrequently used data, it can supplant more frequently accessed data, causing a deterioration in performance for the database\u2019s regular workload.<\/p>\n\n\n\n<p><strong>Data Import, Export, and Backup Operations<\/strong> &#8211; For resilient and non-disruptive backups, use a file system or block-level disk snapshot function, such as the methods described in the MongoDB Backup Methods document. The tools and operations discussed provide functionality that is useful in the context of providing some kinds of backups.<\/p>\n\n\n\n<p>In contrast, use import and export tools to backup a small subset of your data or to move data to or from a third party system. These backups may capture a small crucial set of data or a frequently modified section of data for extra insurance, or for ease of access. mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity.<\/p>\n\n\n\n<p>No matter how you decide to import or export your data, consider the following guidelines:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Label files so that you can identify the contents of the export or backup as well as the point in time the export\/backup reflect.<\/li>\n\n\n\n<li>Do not create or apply exports if the backup process itself will have an adverse effect on a production system.<\/li>\n\n\n\n<li>Make sure that they reflect a consistent data state. Export or backup processes can impact data integrity (i.e. type fidelity) and consistency if updates continue during the backup process.<\/li>\n\n\n\n<li>Test backups and exports by restoring and importing to ensure that the backups are useful.<\/li>\n<\/ul>\n\n\n\n<p><strong>Human Intelligible Import\/Export Formats<\/strong> &#8211; This section describes a process to import\/export a collection to a file in a JSON or CSV format. The examples in this section use the MongoDB tools mongoimport and mongoexport. These tools may also be useful for importing data into a MongoDB database from third party applications. If you want to simply copy a database or collection from one instance to another, consider using the copydb, clone, or cloneCollection commands, which may be more suited to this task. The mongo shell provides the db.copyDatabase() method.<\/p>\n\n\n\n<p>Collection Export with mongoexport &#8211; mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity. With the mongoexport utility you can create a backup file. In the most simple invocation, the command takes the following form:<\/p>\n\n\n\n<p>mongoexport &#8211;collection collection &#8211;out collection.json<\/p>\n\n\n\n<p>This will export all documents in the collection named collection into the file collection.json. Without the output specification (i.e. \u201c&#8211;out collection.json\u201d), mongoexport writes output to standard output (i.e. \u201cstdout\u201d). You can further narrow the results by supplying a query filter using the \u201c&#8211;query\u201d and limit results to a single database using the \u201c&#8211;db\u201d option. For instance:<\/p>\n\n\n\n<p>mongoexport &#8211;db sales &#8211;collection contacts &#8211;query &#8216;{&#8220;field&#8221;: 1}&#8217;<\/p>\n\n\n\n<p>This command returns all documents in the sales database\u2019s contacts collection, with a field named field with a value of 1. Enclose the query in single quotes (e.g. &#8216;) to ensure that it does not interact with your shell environment. The resulting documents will return on standard output. By default, mongoexport returns one JSON document per MongoDB document. Specify the \u201c&#8211;jsonArray\u201d argument to return the export as a single JSON array. Use the \u201c&#8211;csv\u201d file to return the result in CSV (comma separated values) format. If your mongod instance is not running, you can use the \u201c&#8211;dbpath\u201d option to specify the location to your MongoDB instance\u2019s database files. See the following example:<\/p>\n\n\n\n<p>mongoexport &#8211;db sales &#8211;collection contacts &#8211;dbpath \/srv\/MongoDB\/<\/p>\n\n\n\n<p>This reads the data files directly. This locks the data directory to prevent conflicting writes. The mongod process must not be running or attached to these data files when you run mongoexport in this configuration. The \u201c&#8211;host\u201d and \u201c&#8211;port\u201d options allow you to specify a non-local host to connect to capture the export. Consider the following example:<\/p>\n\n\n\n<p>mongoexport &#8211;host mongodb1.example.net &#8211;port 37017 &#8211;username user &#8211;password pass &#8211;collection contacts &#8211;out mdb1-examplenet.json<\/p>\n\n\n\n<p>On any mongoexport command you may, as above specify username and password credentials as above.<\/p>\n\n\n\n<p>Collection Import with mongoimport &#8211; mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity. To restore a backup taken with mongoexport. Most of the arguments to mongoexport also exist for mongoimport. Consider the following command:<\/p>\n\n\n\n<p>mongoimport &#8211;collection collection &#8211;file collection.json<\/p>\n\n\n\n<p>This imports the contents of the file collection.json into the collection named collection. If you do not specify a file with the \u201c&#8211;file\u201d option, mongoimport accepts input over standard input (e.g. \u201cstdin.\u201d) If you specify the \u201c&#8211;upsert\u201d option, all of mongoimport operations will attempt to update existing documents in the database and insert other documents. This option will cause some performance impact depending on your configuration.<\/p>\n\n\n\n<p>You can specify the database option &#8211;db to import these documents to a particular database. If your MongoDB instance is not running, use the \u201c&#8211;dbpath\u201d option to specify the location of your MongoDB instance\u2019s database files. Consider using the \u201c&#8211;journal\u201d option to ensure that mongoimport records its operations in the journal. The mongod process must not be running or attached to these data files when you run mongoimport in this configuration. Use the \u201c&#8211;ignoreBlanks\u201d option to ignore blank fields. For CSV and TSV imports, this option provides the desired functionality in most cases: it avoids inserting blank fields in MongoDB documents.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Apply for MongoDB Certification Now!!<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/databases\/mongodb-server-administrator\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.vskills.in\/certification\/databases\/mongodb-server-administrator<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-mongodb-professional\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Back to Tutorial<\/strong><\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>Import and Export MongoDB Data The export and import tools are useful when you want to backup or export a portion of your data without capturing the state of the entire database, or for simple data ingestion cases. For more complex data migration tasks, you may want to write your own import and export scripts&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[73],"tags":[],"class_list":["post-111792","page","type-page","status-publish","hentry","category-mongodb"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Import and Export MongoDB Data - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Import and Export MongoDB Data - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Import and Export MongoDB Data The export and import tools are useful when you want to backup or export a portion of your data without capturing the state of the entire database, or for simple data ingestion cases. For more complex data migration tasks, you may want to write your own import and export scripts...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T09:01:36+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/\",\"name\":\"Import and Export MongoDB Data - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2021-03-08T05:32:11+00:00\",\"dateModified\":\"2024-04-12T09:01:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Import and Export MongoDB Data\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Import and Export MongoDB Data - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/","og_locale":"en_US","og_type":"article","og_title":"Import and Export MongoDB Data - Tutorial","og_description":"Import and Export MongoDB Data The export and import tools are useful when you want to backup or export a portion of your data without capturing the state of the entire database, or for simple data ingestion cases. For more complex data migration tasks, you may want to write your own import and export scripts...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T09:01:36+00:00","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/","name":"Import and Export MongoDB Data - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2021-03-08T05:32:11+00:00","dateModified":"2024-04-12T09:01:36+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/import-and-export-mongodb-data\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Import and Export MongoDB Data"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/111792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=111792"}],"version-history":[{"count":3,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/111792\/revisions"}],"predecessor-version":[{"id":132016,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/111792\/revisions\/132016"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=111792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=111792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=111792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}