Working with Data Import and Export
1. Using mongoimport for JSON/CSV
mongoimport --uri=... --db=app --collection=users --file=users.json
2. Using mongoexport for JSON/CSV
mongoexport --uri=... --db=app --collection=users --out=users.json
3. Importing from JSON File
| Flag | Detail |
| --jsonArray | When file is a single JSON array |
| Default | JSON lines (one doc per line) |
| Flag | Detail |
| --type=json (default) | JSON lines |
| --jsonArray | Wrap output as array |
| --pretty | Indented output |
5. Importing from CSV File
| Flag | Detail |
| --type=csv | CSV mode |
| --headerline | First row are field names |
| --fields / --fieldFile | Explicit schema |
| Flag | Detail |
| --type=csv | CSV mode |
| --fields a,b,c | Required for CSV export |
7. Setting Import Field Types
| Form | Example |
| field.type() | --columnsHaveTypes --fields="age.int32(),name.string()" |
| Types | int32, int64, double, decimal, date, boolean, binary |
8. Using Query for Selective Export
mongoexport ... --query='{"status":"active"}' --out=active.json
9. Handling Large Data Imports
| Flag | Detail |
| --numInsertionWorkers N | Parallel inserts |
| --batchSize | Tune insert batches |
| --maintainInsertionOrder | Default false for throughput |
10. Using --drop for Clean Import
| Flag | Detail |
| --drop | Drop target collection before importing |
| --mode upsert | Update if _id exists |