#csv #mongo-db #import #cli #debugging

app fimo

A command-line tool to import CSV files into MongoDB

1 unstable release

new 0.1.0 Apr 21, 2025

#1100 in Database interfaces

Download history 87/week @ 2025-04-16

87 downloads per month

MIT license

24KB
367 lines

fimo (file-mongo)

Fimo is a fast and flexible CLI tool written in Rust that transforms CSV data into MongoDB documents using YAML-based field mappings and Jinja2-style templating. It's ideal for bulk inserts, updates, and upserts with full control over document structure.


๐Ÿš€ Features

  • โœ… RFC 4180-compliant CSV parsing (including headers, quoting, escaped quotes)
  • ๐Ÿ› ๏ธ Field mapping via YAML configuration
  • ๐Ÿง  Custom transformation logic using MiniJinja
  • ๐Ÿ“ฆ MongoDB insert, update, and upsert support
  • ๐Ÿงช Validate-only and dry-run modes
  • ๐Ÿ”„ Batch processing support for large files
  • ๐Ÿ” Supports Extended JSON and BSON types
  • ๐Ÿ”ฃ Configurable CSV delimiter and quote characters
  • ๐Ÿ“Š Debug and verbose output for development and testing

๐Ÿ“ฆ Installation

cargo install fimo

Or clone and build:

git clone https://github.com/fimo-org/fimo.git
cd fimo
cargo build --release

๐Ÿ“ Usage

fimo \
  --input tests/data/extended.csv \
  --mapping tests/mapping/extended.yaml \
  --template-dir tests/templates \
  --mongo-uri mongodb://localhost:27017 \
  --db testdb \
  --collection testcol \
  --operation upsert \
  --extended-json \
  --debug

๐Ÿงช Example: With Templates and Extended JSON

๐Ÿ“ data.csv

_id,price,created_at,name,active
507f1f77bcf86cd799439011,12.34,2024-01-01T10:00:00Z,Alice,yes

๐Ÿงฉ mapping.yaml

_id:
  type: objectId
price:
  type: decimal
created_at:
  type: date
name:
  type: string
active:
  type: bool
  truthy: ["yes", "true", "1", "Y"]
  falsy: ["no", "false", "0", "N"]

๐Ÿงพ templates/upsert.j2

{
  "filter": { "_id": {{ row._id }} },
  "update": {
    "$set": {
      "price": {{ row.price }},
      "created_at": {{ row.created_at }},
      "name": "{{ row.name }}",
      "active": {{ row.active }}
    },
    "$setOnInsert": {
      "created_at": {{ row.created_at }}
    }
  }
}

โ–ถ๏ธ Run the Import

fimo \
  --input data.csv \
  --mapping mapping.yaml \
  --template-dir templates \
  --mongo-uri mongodb://localhost:27017 \
  --db testdb \
  --collection customers \
  --operation upsert \
  --extended-json

๐Ÿงช Example: Raw Insert (No Templates)

๐Ÿ“ simple.csv

name,age,active
Bob,25,true

๐Ÿงฉ simple.yaml

name:
  type: string
age:
  type: int
active:
  type: bool

โ–ถ๏ธ Raw Insert Command

fimo \
  --input simple.csv \
  --mapping simple.yaml \
  --mongo-uri mongodb://localhost:27017 \
  --db demo \
  --collection people \
  --operation insert \
  --raw-insert

๐Ÿ”ง CLI Options

Option Description
--input Path to the CSV file
--mapping Path to YAML mapping file
--mongo-uri MongoDB connection URI
--db MongoDB database name
--collection MongoDB collection name
--operation insert, update, or upsert
--batch-size Number of docs to write in bulk (default: 0)
--no-header Use autogenerated headerscol_0, col_1...
--delimiter CSV delimiter (default:,)
--quote CSV quote character (default:")
--template-dir Directory with Jinja templates
--extended-json Enable support for non-JSON BSON values
--validate-only Validate rows without writing to MongoDB
--dry-run Print documents instead of inserting
--debug Enable verbose output

๐Ÿง  Truthy/Falsy Mapping for Booleans

In mapping.yaml, you can define per-field truthy/falsy values:

active:
  type: bool
  truthy: ["yes", "1", "true"]
  falsy: ["no", "0", "false"]

This allows more natural mapping from "yes"/"no", "Y"/"N" strings into true/false.

๐Ÿ“ Project Structure

.
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main.rs             # CLI entry point
โ”‚   โ”œโ”€โ”€ cli.rs              # Command-line argument parsing
โ”‚   โ”œโ”€โ”€ mongo.rs            # MongoDB connection
โ”‚   โ”œโ”€โ”€ transform.rs        # Mapping, templating, BSON conversion
โ”‚   โ”œโ”€โ”€ mapping.rs          # YAML field type parsing
โ”‚   โ””โ”€โ”€ template.rs         # Jinja environment loader
โ”œโ”€โ”€ mappings/               # Sample mapping YAML files
โ”œโ”€โ”€ templates/              # Sample Jinja templates
โ”œโ”€โ”€ tests/                  # Sample CSV input for testing
โ””โ”€โ”€ Cargo.toml              # Project manifest

๐Ÿ“š RFC 4180 Compatibility

Fimo is fully compatible with RFC 4180:

  • Comma-separated fields (configurable)
  • Quoted fields with escape support
  • Optional headers
  • Uniform field count (recommended but not enforced)

๐Ÿ“œ License

MIT ยฉ

Dependencies

~19โ€“30MB
~464K SLoC