How to Import JSON to MongoDB (3 Methods)
MongoDB stores data as BSON (Binary JSON), so importing JSON is a natural operation. The primary tool is mongoimport, part of the MongoDB Database Tools package.
Method 1: mongoimport CLI
Install MongoDB Database Tools if you don't have them (they're separate from the server since MongoDB 4.4):
# macOS
brew install mongodb-database-tools
# Ubuntu/Debian
sudo apt-get install mongodb-database-toolsImport a JSON array file:
mongoimport --uri="mongodb://localhost:27017/mydb" \
--collection=users \
--file=users.json \
--jsonArrayThe --jsonArray flag tells mongoimport the file contains a single JSON array of documents.
Import NDJSON (one document per line):
mongoimport --uri="mongodb://localhost:27017/mydb" \
--collection=events \
--file=events.ndjsonNo --jsonArray flag needed -- mongoimport defaults to expecting one document per line.
Import to MongoDB Atlas:
mongoimport --uri="mongodb+srv://user:pass@cluster.mongodb.net/mydb" \
--collection=users \
--file=users.json \
--jsonArrayUseful options:
--drop-- drop the collection before importing--upsertFields=email-- update existing documents matching on theemailfield--mode=upsert-- insert new documents, update existing ones--numInsertionWorkers=4-- parallelize inserts for large files--batchSize=1000-- number of documents per batch
mongoimport --uri="mongodb://localhost:27017/mydb" \
--collection=users \
--file=users.json \
--jsonArray \
--mode=upsert \
--upsertFields=email \
--numInsertionWorkers=4Method 2: insertMany in mongosh
For smaller datasets or programmatic imports, use the MongoDB Shell:
// mongosh
const data = JSON.parse(cat("users.json"));
db.users.insertMany(data);Or with Node.js:
const { MongoClient } = require("mongodb");
const fs = require("fs");
async function importJSON() {
const client = new MongoClient("mongodb://localhost:27017");
await client.connect();
const db = client.db("mydb");
const records = JSON.parse(fs.readFileSync("users.json", "utf8"));
const result = await db.collection("users").insertMany(records, {
ordered: false, // continue on duplicate key errors
});
console.log(`Inserted ${result.insertedCount} documents`);
await client.close();
}
importJSON();The ordered: false option is important for bulk imports -- it continues inserting even if some documents fail (e.g., duplicate _id values), instead of stopping at the first error.
Method 3: Mako (GUI)
Mako connects to MongoDB and provides a visual import interface. It handles JSON parsing and lets you preview documents before inserting them into a collection.
JSON vs Extended JSON
MongoDB uses Extended JSON to represent BSON types that don't exist in standard JSON:
{
"_id": { "$oid": "507f1f77bcf86cd799439011" },
"created": { "$date": "2026-01-15T10:30:00Z" },
"count": { "$numberLong": "9999999999" }
}mongoimport handles both standard JSON and Extended JSON automatically. If your data comes from mongoexport, it's already in Extended JSON format and imports cleanly.
Common Gotchas
_id field. If your JSON documents don't have an _id field, MongoDB auto-generates an ObjectId for each document. If they do have _id, duplicates will fail unless you use --mode=upsert.
Date handling. Standard JSON has no date type. Strings like "2026-01-15" are stored as strings, not BSON dates. To import as dates, use Extended JSON format:
{ "created": { "$date": "2026-01-15T00:00:00Z" } }Or transform after import:
db.users.updateMany({}, [
{ $set: { created: { $dateFromString: { dateString: "$created" } } } },
]);Nested arrays and objects. MongoDB handles these natively -- no flattening needed. A JSON document with nested objects and arrays imports directly as a BSON document with the same structure.
Large files. For files over 1 GB, mongoimport with --numInsertionWorkers=4 or higher parallelizes the insert. The tool streams the file, so memory usage stays low regardless of file size.
Number types. JSON numbers are imported as either doubles or 32-bit integers. For 64-bit integers, use Extended JSON:
{ "amount": { "$numberLong": "9007199254740993" } }Without this, large integers lose precision when stored as doubles.
Mako connects to MongoDB, PostgreSQL, MySQL, BigQuery, Snowflake, and ClickHouse with AI-powered autocomplete. Try it free at mako.ai.