mongoexport
Export MongoDB data to external formats
TLDR
Export a collection to stdout, formatted as JSON
Export the documents in the specified collection that match a query to a JSON file
Export documents as a JSON array instead of one object per line
Export documents to a CSV file
Export documents that match the query in the specified file to a CSV file, omitting the list of field names on the first line
Export documents to stdout, formatted as human-readable JSON
Display help
SYNOPSIS
mongoexport [connection-options] [export-options] --db <database> --collection <collection> [--out <file>]
Common connection options include:
--host <hostname> --port <port> --username <username> --password <password> --authenticationDatabase <database>
Common export options include:
--query <JSON> --fields <field1,field2,...> --type <json|csv> --jsonArray --pretty --limit <number> --sort <JSON>
PARAMETERS
--help
Displays detailed help information about the command.
--version
Displays the version of the mongoexport utility.
--verbose, -v
Increases the verbosity of the output. Use multiple -v for more verbose output.
--quiet
Suppresses all but critical messages.
--uri <connection string>
Specifies the MongoDB connection string. Overrides --host, --port, --username, etc.
--host, -h <hostname>
Specifies the hostname or IP address of the MongoDB instance to connect to.
--port <port>
Specifies the port on which the MongoDB instance is listening. Default is 27017.
--username, -u <username>
Specifies a username to authenticate to the MongoDB instance.
--password, -p <password>
Specifies a password to authenticate to the MongoDB instance. If omitted with --username, mongoexport will prompt for a password.
--authenticationDatabase <database>
Specifies the database to authenticate against if different from the target database.
--db, -d <database>
(Required) Specifies the database containing the collection to export.
--collection, -c <collection>
(Required) Specifies the collection to export documents from.
--query, -q <JSON>
Provides a JSON document as a query to filter documents in the collection.
--queryFile <filename>
Specifies a file containing a JSON query to filter documents.
--fields, -f <field1,field2,...>
Specifies a comma-separated list of field names to include in the export.
--fieldFile <filename>
Specifies a file containing a list of field names (one per line) to include.
--type <json|csv>
Specifies the output format. Can be json (default) or csv.
--out, -o <filename>
Specifies the file to which the exported data will be written. If omitted, data goes to stdout.
--jsonFormat <canonical|relaxed|legacy>
Specifies the JSON output format: canonical (strict BSON representation), relaxed (default, for human readability), or legacy (MongoDB 2.x extended JSON).
--jsonArray
Outputs the entire collection as a single JSON array, where each document is an element.
--pretty
Prints the output in a human-readable format, with indentation.
--noHeaderLine
Prevents mongoexport from including the header line for CSV output.
--limit <number>
Specifies the maximum number of documents to export.
--skip <number>
Specifies the number of documents to skip before starting the export.
--sort <JSON>
Provides a JSON document to specify the order in which to export documents (e.g., '{ "field": 1 }' for ascending).
--forceTableScan
Forces a table scan of the collection. Useful for avoiding a query for some queries, but generally less performant.
--readPreference <mode>
Specifies the read preference mode (e.g., primary, secondaryPreferred) for replica sets.
DESCRIPTION
mongoexport is a command-line utility for exporting data from a MongoDB instance to standard output or a specified file. It is part of the MongoDB Database Tools, a separate package from the core MongoDB server.
The tool allows users to extract documents from a specific collection within a database. By default, mongoexport outputs data in JSON format, supporting both relaxed (default for easy parsing) and canonical (BSON-like strict) JSON representations. It can also export data in CSV (Comma Separated Values) format, which is particularly useful for importing into spreadsheet applications or other data analysis tools.
Users can filter the exported data using a JSON query, specify which fields to include or exclude, and even control the number of documents, starting point, and sort order. This flexibility makes mongoexport a valuable tool for data backups, data migration, creating datasets for analysis, or generating reports from MongoDB collections. It's often used in conjunction with mongoimport for data transfers between different MongoDB deployments or systems.
CAVEATS
mongoexport is designed for data export, not full database backups. It does not export indexes, user roles, stored JavaScript, or other database commands. For full database or collection backups including indexes and other metadata, use mongodump.
When exporting to CSV format, ensure that the selected fields have a consistent schema across documents, or explicitly define all fields to avoid missing data. If a field is specified but not present in a document, it will be exported as an empty value.
Exporting very large collections without filtering can consume significant memory and time on both the client and server. Consider using --query, --limit, or --sort for more efficient exports. Passwords provided directly on the command line might be visible in process listings (e.g., ps -ef). Consider using a configuration file or interactive password prompts for better security.
EXAMPLE USAGE
To export all documents from 'my_collection' in 'my_database' to a file named 'data.json' in pretty-printed JSON format:
mongoexport --db my_database --collection my_collection --out data.json --pretty
To export specific fields 'name' and 'age' for documents where 'status' is 'active' to CSV format:
mongoexport --db my_database --collection my_collection --query '{ "status": "active" }' --fields name,age --type csv --out active_users.csv --noHeaderLine
SECURITY CONSIDERATIONS
Avoid providing sensitive information like passwords directly on the command line if possible, as they can be exposed in system process lists. For production environments, consider using environment variables, a configuration file (with --config), or allow mongoexport to prompt for a password interactively if supported by your version.
HISTORY
mongoexport is a key component of the MongoDB Database Tools, which were originally bundled with the MongoDB server distribution but were separated into a standalone package starting with MongoDB 4.2. This separation allows the tools to be updated independently of the database server, providing more flexibility.
Over its development, mongoexport has gained significant features, including enhanced authentication mechanisms (e.g., SCRAM-SHA-256, Kerberos), improved SSL/TLS support, and finer control over JSON output formats (relaxed, canonical, legacy). Its evolution reflects the growing complexity and security requirements of modern MongoDB deployments, maintaining its role as a fundamental utility for data handling.
SEE ALSO
mongoimport(1), mongodump(1), mongorestore(1), mongosh(1)