How to use the command 'mongoexport' (with examples)
Mongoexport is a command-line tool that allows users to export data stored in a MongoDB instance in various formats such as JSON or CSV. It provides flexibility to export specific collections or documents based on queries and supports different output options. This article will provide examples and explanations for different use cases of the ‘mongoexport’ command.
Use case 1: Export a collection to stdout
, formatted as JSON
Code:
mongoexport --uri=connection_string --collection=collection_name
Motivation: This use case is helpful when you want to export the entire collection to the console or redirect the output to another process or file. It can be used for various purposes such as debugging or transferring data.
Explanation:
--uri
: Specifies the MongoDB connection string.--collection
: Specifies the name of the collection to be exported.
Example Output:
{"_id":{"$oid":"614fe31c3481de7c4838e2bc"},"name":"John Doe","age":30}
{"_id":{"$oid":"614fe3433481de7c4838e2bd"},"name":"Jane Smith","age":25}
Use case 2: Export the documents matching a query to a JSON file
Code:
mongoexport --db=database_name --collection=collection_name --query="query_object" --out=path/to/file.json
Motivation: This use case is useful when you only need to export specific documents from a collection based on a query. It allows you to create a filtered export for further analysis or sharing.
Explanation:
--db
: Specifies the name of the database containing the collection.--collection
: Specifies the name of the collection to be exported.--query
: Specifies the query object to match the desired documents.--out
: Specifies the path and filename for the output JSON file.
Example Output:
A file named file.json
will be created at the specified path containing the exported documents matching the specified query.
Use case 3: Export documents as a JSON array instead of one object per line
Code:
mongoexport --collection=collection_name --jsonArray
Motivation: By default, ‘mongoexport’ exports each document as a separate JSON object per line. This use case is helpful when you want to export all the documents as a single JSON array.
Explanation:
--collection
: Specifies the name of the collection to be exported.--jsonArray
: Specifies that the exported documents should be formatted as a JSON array.
Example Output:
[
{"_id":{"$oid":"614fe31c3481de7c4838e2bc"},"name":"John Doe","age":30},
{"_id":{"$oid":"614fe3433481de7c4838e2bd"},"name":"Jane Smith","age":25}
]
Use case 4: Export documents to a CSV file
Code:
mongoexport --collection=collection_name --type=csv --fields="field1,field2,..." --out=path/to/file.csv
Motivation: Sometimes, it is necessary to export data in CSV format for compatibility with other tools or systems. This use case allows you to export the specified fields from a collection to a CSV file.
Explanation:
--collection
: Specifies the name of the collection to be exported.--type
: Specifies the export format. In this case, it is set to ‘csv’.--fields
: Specifies the fields to be exported. You can provide a comma-separated list of field names.--out
: Specifies the path and filename for the output CSV file.
Example Output:
A file named file.csv
will be created at the specified path containing the exported documents’ specified fields in CSV format.
Use case 5: Export documents matching a query to a CSV file, omitting field names on the first line
Code:
mongoexport --collection=collection_name --type=csv --fields="field1,field2,..." --queryFile=path/to/file --noHeaderLine --out=path/to/file.csv
Motivation: In some cases, you may want to export data to a CSV file without including a header line (field names) to simplify data processing or importing into other systems.
Explanation:
--collection
: Specifies the name of the collection to be exported.--type
: Specifies the export format. In this case, it is set to ‘csv’.--fields
: Specifies the fields to be exported. You can provide a comma-separated list of field names.--queryFile
: Specifies the path to a file containing the query object to match the desired documents.--noHeaderLine
: Specifies that the output CSV file should not include the field names on the first line.--out
: Specifies the path and filename for the output CSV file.
Example Output:
A file named file.csv
will be created at the specified path, containing the exported documents’ specified fields without the field names on the first line.
Use case 6: Export documents to stdout
, formatted as human-readable JSON
Code:
mongoexport --uri=mongodb_uri --collection=collection_name --pretty
Motivation: This use case is helpful when you want to view the exported documents in a human-readable format directly on the console rather than a file. It can be used for quick inspection or debugging.
Explanation:
--uri
: Specifies the MongoDB connection URI.--collection
: Specifies the name of the collection to be exported.--pretty
: Specifies that the exported documents should be formatted in a human-readable JSON format.
Example Output:
{
"_id": {
"$oid": "614fe31c3481de7c4838e2bc"
},
"name": "John Doe",
"age": 30
}
{
"_id": {
"$oid": "614fe3433481de7c4838e2bd"
},
"name": "Jane Smith",
"age": 25
}
Use case 7: Display help
Code:
mongoexport --help
Motivation: If you need information about the available options and usage of the ‘mongoexport’ command, this use case will display the help documentation.
Example Output: Help documentation for the ‘mongoexport’ command will be displayed, providing details about the various options, arguments, and usage examples.