Data Export and Import in MongoDB

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:05:00

Data Export and Import

Data Export and Import are the processes of extracting data from a database (export) and loading data into a database (import). These actions are essential for tasks like data backup, migration, and analysis.

Data Export and Import: MongoDB Compass

Data Export

MongoDB Compass provides a simple interface for exporting data from MongoDB databases and collections. Users can choose the database and collection from which they wish to export data, the export format (e.g., JSON, CSV), and the export settings. MongoDB Compass makes the export process easier by giving users a visual interface to navigate and choose data for export.

Data Import

MongoDB Compass also allows you to import data into MongoDB databases. Users can indicate the database into which they want to import data and the file holding the data. MongoDB Compass provides options for importing data, such as dropping existing collections and considering the first line of the input file as field names.

Data Export and Import: Command Prompt

Data Export

Using the mongoexport command, users can export data from MongoDB databases via the Command Prompt, or terminal on Unix-based systems. Users can specify the database and collection from which to export data, the export type (e.g., JSON, CSV), and the output file location. Customize the export process by specifying additional options, such as query criteria.

Data Import

The command Prompt allows users to import data into MongoDB databases by running the mongoimport command. Users can choose the database and collection into which they want to import data, as well as the input format (e.g., JSON, CSV) and file location. Importing data options, such as upsert behavior and field mapping, can be set to modify the import process to individual needs.

Import/Export Collection

Mongoexport

Using the mongoexport command-line tool in MongoDB, you can export data in JSON or CSV format to a file from a MongoDB database collection. It allows you to extract subsets of data based on query parameters or export a whole collection.

Key Options with the mongoexport Command

  • -d: The -d option specifies the database name from which to export data.
  • -c: The -c option specifies the name of the collection from which to export data.
  • -o: The -o option specifies the output file for the exported data.
  • -q: The -q option allows you to specify a query to filter the exported data.
  • --type: The --type option sets the output format, which can be either JSON or CSV.
  • --fields: specifies which fields should be included in the exported data.
  • --sort: arranges the exported data according to the provided fields and order.
  • --limit: restricts the amount of exported documents to n.

MongoExport to Raw JSON File

When you run the mongoexport command to export data from a MongoDB database collection, the default output format is a JSON file. This file contains the exported data in JSON format, which is readily processed and analyzed with a variety of tools.

MongoExport to CSV File

Mongoexport can export data to a CSV file as well as a JSON file by specifying the --type=csv option. This is useful if you wish to analyze your data in spreadsheet software such as Microsoft Excel or Google Sheets.

Mongoexport and Options

  • --db: Enter the name of the database from which to export data.
  • --collection: Enter the name of the collection from which to export data.
  • --out: Sets the name and location of the output file. If the file already exists, MongoExport will overwrite it. If this option is not given, mongoexport displays the data in the console.
  • --type: This specifies the output file format. Formats supported include JSON, CSV, tsv, and bson.
  • --fields: Specifies which fields should be exported. This option accepts comma-separated field names. If this option is not set, all fields are exported.
  • --query: Specifies a query for filtering the exported data. This option accepts a JSON object representing the query. Only documents matching the query are exported.
  • --sort: Sets the sort order for the exported data. This option accepts a JSON object representing the sort order. The exported data is sorted in the chosen order.
  • --limit: Determines the maximum number of documents to export.
  • --skip: Determines the number of documents to skip before exporting data.
  • --username and --password: Enter the username and password to authenticate with the database.
  • --host and --port: This specifies the MongoDB server's hostname and port number. If these settings are not supplied, mongoexport will use localhost and the default port (27017).

Mongoimport

Mongoimport from a JSON file. mongoimport is a command-line program for importing data into a MongoDB database collection from a file in a variety of formats. One of the most used data import formats is JSON, which is a text-based format that hierarchically encodes data.

Mongodump

Mongodump is a command-line tool from MongoDB that exports a database or collection to a binary format. Mongodump produces a series of BSON files that can be used to restore data to a MongoDB instance with the Mongorestore tool.

Mongorestore

Mongorestore is a command-line tool supplied by MongoDB that allows you to restore a binary export of a MongoDB database or a specific collection generated with the Mongodump tool. The input to Mongorestore is a collection of BSON files generated by Mongodump.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this