Problem Statement
Which MongoDB tools are used for importing and exporting data?
Explanation
Mongoimport and mongoexport are command-line utilities for importing and exporting data. Mongoexport exports data from MongoDB collections to JSON or CSV files, while mongoimport imports data from JSON, CSV, or TSV files into MongoDB collections.
These tools are useful for data migration, backups of small datasets, sharing data between systems, and loading test data. For larger datasets or full backups, mongodump and mongorestore are more appropriate as they work with BSON format.
Code Solution
SolutionRead Only
// Export collection to JSON mongoexport --db=mydb --collection=users --out=users.json // Export to CSV with specific fields mongoexport --db=mydb --collection=users \ --type=csv --fields=name,email,age --out=users.csv // Import JSON data mongoimport --db=mydb --collection=users --file=users.json // Import CSV with header row mongoimport --db=mydb --collection=users \ --type=csv --headerline --file=users.csv // Import with upsert (update existing or insert new) mongoimport --db=mydb --collection=users \ --file=users.json --mode=upsert
