mongoimport: Import JSON, CSV, and TSV Data

To load a data file into MongoDB, choose its destination database and collection. mongoimport is a command-line utility that reads data files and writes their records to a MongoDB deployment.

This article shows how to set the format options and check the import counts.

TL;DR

Use mongoimport from your system terminal to load JSON, CSV, or TSV into a named MongoDB database and collection.

  • Install MongoDB Database Tools separately if the command is missing.
  • Choose `–jsonArray` for one JSON array and `–headerline` for CSV or TSV headers.
  • Default imports insert documents. Choose a matching mode explicitly when re-importing records.

What Is mongoimport?

mongoimport is a command-line utility in MongoDB Database Tools that reads Extended JSON, CSV, or TSV and writes its records to a MongoDB deployment. Run it in your operating-system terminal, not inside mongosh.

MongoDB Server and Database Tools are separate downloads in modern releases, and the tools have their own version numbers. The current MongoDB documentation identifies mongoimport 100.20.1. Confirm the version available for your platform on the official Database Tools installation page, rather than copying an old server-version folder path.

mongoimport --version

This checks whether the executable is installed and on PATH. Bash returned “mongoimport: command not found,” so install Database Tools or add its executable directory to PATH before importing.

Command output:

/usr/bin/bash: line 1: mongoimport: command not found

Terminal reports that mongoimport is not found

MongoDB’s guide shows the command-line form as:

How to Import Data with mongoimport Step by Step

First identify the file’s structure, then point mongoimport at the correct deployment and destination. Use the same installed Database Tools version across related export and import work where practical.

Step 1: Check the tool and choose the destination

Check the executable first. I ran the version check above, and its output shows the shell cannot locate mongoimport.

mongoimport --version
/usr/bin/bash: line 1: mongoimport: command not found
Terminal shows mongoimport version check and command not found output

Set a database and collection explicitly so the import does not depend on a filename-derived collection name:

mongoimport --uri="mongodb://localhost:27017" --db=inventory --collection=items --file=items.json

For an authenticated or hosted deployment, use its connection URI in place of the local URI. Avoid putting a password directly in shell history. Use your environment’s approved secret handling, and confirm the account has write access to the target database.

Step 2: Import JSON objects or an array

For a file containing one array of objects, add the array option:

mongoimport --uri="mongodb://localhost:27017" --db=inventory --collection=items --file=items-array.json --jsonArray

MongoDB documents a 16 MB limit for `–jsonArray` imports. For larger datasets, newline-delimited documents avoid treating the whole input as one array value. Extended JSON input should use the supported v2 format unless the source requires the legacy format option.

Step 3: Map CSV or TSV columns

Choose the delimiter and tell mongoimport where the column names come from.

mongoimport --uri="mongodb://localhost:27017" --db=inventory --collection=items --type=csv --headerline --file=items.csv

Use `–type=tsv` for tab-separated input. MongoDB accepts `–headerline` with CSV or TSV. Without a header, supply the column names with `–fields` in a command.

Step 4: Read the result and verify the collection

Review mongoimport’s summary for imported and failed document counts, then query the destination collection with mongosh. For example, run `mongosh “mongodb://localhost:27017/inventory”` and check `db.items.findOne()` plus `db.items.countDocuments()`.

A nonzero count alone does not prove every record was accepted. Compare it with the source row count, inspect representative fields, and read the command’s error details before retrying. MongoDB’s mongoimport guide includes successful output examples and format-specific commands.

Handle Existing Data and Import Errors

The default mode inserts records, so repeated imports can produce duplicate-key errors or duplicate records when identifiers differ. Choose the matching behavior and scope before running the command again.

SituationOption or symptomWhat to do
Load new recordsDefault insertUse unique identifiers and review duplicate-key errors.
Replace matching documents--mode=upsertMatch by _id or specify stable keys with --upsertFields.
Keep old fields while adding incoming fields--mode=mergeConfirm the merge key identifies only the intended document.
Replace the whole collection--dropUse only when deleting the collection and its indexes is intended.
Command not foundTool missing or outside PATHInstall Database Tools and check the executable path.
Headerline rejectedInput still treated as JSONSet --type=csv or --type=tsv and check the file delimiter.

Upsert replaces matching documents, while merge updates supplied fields and retains others. Both modes insert nonmatching documents.

The drop option removes the destination collection and its indexes before loading. For an export in the opposite direction, see how to export MongoDB collections. For an application-level update, see findOneAndUpdate in MongoDB.

Conclusion

Check the command summary and query the collection after loading.

Use the official mongoimport reference for the full option list and the installation guide for platform-specific setup.

FAQ

These answers cover common format and repeat-import questions.

Does mongoimport overwrite a collection?

By default, mongoimport inserts documents and does not replace the whole collection. Use –drop only when you intend to remove the collection and its indexes before importing.

How do I import a CSV file with headers?

Specify –type=csv and –headerline so mongoimport uses the first row as field names. For TSV, use –type=tsv.

Why does mongoimport say command not found?

The Database Tools executable may not be installed or its directory may not be on PATH. Install the tools for your platform, then check the executable path.

Aneesha S
Aneesha S

Aneesha S writes practical guides to MongoDB, Mongoose, and Node.js. Her articles cover document queries and updates, file operations, and HTTP requests.

Articles: 169