How-ToJanuary 11, 20267 min read

How to Import CSV into MongoDB: 5 Practical Methods

Five reliable ways to load CSVs into MongoDB—from mongoimport and Compass to pipelines and custom scripts—plus when to choose each.

Igor Nikolic
Igor Nikolic

Co-founder, FileFeed

How to Import CSV into MongoDB: 5 Practical Methods

MongoDB is great for flexible schemas, but CSVs still bring quirks: delimiters, headers, encodings, and type inference. Here are five practical ways to import CSVs into MongoDB, from one-off commands to production-grade flows.

1) mongoimport (CLI)

The fastest way from CSV to a collection. Ideal for quick loads when you have CLI access.

  • Best when: you have shell access, one-off or scripted loads, moderate file sizes.

mongoimport \
  --uri="mongodb://localhost:27017/app" \
  --collection=users \
  --type=csv \
  --headerline \
  --file=/path/to/users.csv

2) MongoDB Compass (UI)

Compass provides a guided UI import: choose collection, set delimiter/field mappings, preview, import.

  • Best when: non-technical teammates, small/medium files, visual mapping.
  • Great for ad hoc; not ideal for repeat automation.

3) Atlas UI Data Import

If you use Atlas, the web UI supports CSV imports directly to a cluster.

  • Best when: you use Atlas, want no local tooling, small/medium imports.
  • Convenient but manual; for repeats use pipelines/scripts.

4) Pipeline via an ETL Tool (e.g., Airbyte)

ETL tools can pull CSVs from storage or URLs and push into MongoDB on a schedule.

  • Best when: recurring loads, need scheduling/monitoring, low-code setup.
  • Configure source (CSV from S3/GCS/local), set destination MongoDB, map fields, set frequency.

5) Custom Script (Python + pymongo)

For full control—validation, casting, dedupe, retries—use a small script.

pip install pymongo
import csv
from pymongo import MongoClient

client = MongoClient("mongodb://localhost:27017")
col = client.app.users

with open("users.csv", newline="", encoding="utf-8") as f:
    reader = csv.DictReader(f)
    docs = []
    for row in reader:
        docs.append({
            "email": row.get("email"),
            "first_name": row.get("first_name"),
            "last_name": row.get("last_name"),
        })
    if docs:
        col.insert_many(docs)

  • Best when: recurring loads, custom validation/transform, logging/retries needed.

Choosing the Right Approach

  • One-time CLI: mongoimport.
  • Manual UI: Compass or Atlas UI.
  • Scheduled/low-code: ETL tool (e.g., Airbyte).
  • Recurring/controlled: custom script with validation (pymongo).

Where FileFeed Fits

Once CSV imports become part of your product or onboarding, files change shape, validation rules grow, retries and audit logs are needed, and engineers become the bottleneck. FileFeed lets you define validation, mapping, and transformations once, reuse them across customers and environments, and deliver clean, consistent MongoDB data without bespoke glue code.

Final Thoughts

MongoDB makes it easy to store semi-structured data. Pick a simple path for one-offs; invest in repeatable pipelines when imports become recurring or user-facing. FileFeed keeps CSV imports predictable and observable without rebuilding the same logic each time.

Stay Updated

Subscribe to our newsletter and get the latest insights on secure file transfers, automation, and best practices.

Schedule

Ready to automate your file workflows?

Tell us how you exchange files today, and we’ll show you how to replace manual uploads and scripts with a single, automated pipeline.