@kitapp-developers/mongo-import-export
Version:
A modern CLI tool to import/export MongoDB collections to and from JSON or CSV β great for backups, migrations, and local development.
168 lines (109 loc) β’ 3.94 kB
Markdown
# mongo-import-export

[](https://www.npmjs.com/package/@kitapp-developers/mongo-import-export)




## π mongo-import-export
A modern and powerful CLI tool to import and export MongoDB collections to and from JSON and CSV files. Perfect for backups, migrations, and local development.
## β¨ Features & Key Advantages
This tool was built to make MongoDB data management as simple and effective as possible:
### 1. ποΈ Interactive & User-Friendly
- A step-by-step **interactive prompt** guides you through the entire import or export process with clear questions β no need to remember complex commands.
### 2. π Multiple Format Support
- **JSON** β preserves full MongoDB fidelity (e.g., `ObjectId`, `Date`)
- **CSV** β ideal for spreadsheets like Excel or Google Sheets
### 3. π‘οΈ Data Integrity Guarantee
- Each export includes a `manifest.sha256` with checksums
- During import, checksums are verified
- Ensures files havenβt been corrupted or tampered with
### 4. β‘ High Performance with Streaming
- Stream-based processing for JSON files
- Handles datasets of **many gigabytes**
- Keeps memory usage minimal β perfect for large-scale backups/migrations
### 5. π§ Intelligent Database Selection
- If no database is specified in your connection string:
- Connects to MongoDB server
- Lists all available databases
- Lets you choose interactively
### 6. π Advanced Conflict Resolution (During Import)
- `upsert`: Replace if exists, insert if not
- `skip`: Ignore documents that already exist
- `insert` *(default)*: Fail on duplicate `_id`
### 7. π Clear Progress Indicators
- Real-time progress bars per collection
- Clean and informative CLI output
- Helpful for long-running operations
### 8. βοΈ Configurable via `.env` File
Set default values and skip repetitive input:
```env
DATA_FOLDER=./data
BATCH_SIZE=1000
LOG_LEVEL=debug
LOG_FILE=mongo_script_log.txt
```
## π¦ Installation
**Global (recommended for CLI usage):**
```bash
npm install -g @kitapp-developers/mongo-import-export@latest
```
**Local (as project dependency):**
```bash
npm install @kitapp-developers/mongo-import-export@latest
```
## π οΈ Usage
Run the CLI:
```bash
mongo-import-export
```
Or use without global install:
```bash
npx mongo-import-export
```
The CLI will guide you through:
1. **Select Action:** Import or Export
2. **MongoDB URI:**
```bash
mongodb://user:password@host:port/
```
3. **Select Format:** `json` or `csv`
4. **Database Selection:**
- If URI includes a DB, it's used
- Otherwise, select from list
**For Import:**
- Enter target DB name
- Choose whether to clear collections
- Choose conflict resolution strategy: Upsert / Skip / Insert
## π§βπ» Example Workflow
```bash
? Select action: Import data into a database
? Enter MongoDB connection URI: mongodb://localhost:27017
? Select data format: json
? Enter database name to import to: myNewDatabase
? Clear collections before importing? Yes
```
## π¬ Demo

## π Configuration
Optional `.env` file for defaults:
```env
DATA_FOLDER=./data
BATCH_SIZE=1000
LOG_LEVEL=info
LOG_FILE=mongo_script_log.txt
```
Place `.env` in the same directory where you run the CLI.
## βοΈ Requirements
1. Node.js v20 or higher
2. MongoDB server accessible via the provided connection URL
3. Your smile π
## π License
MIT License