Insert and Import Export
Introduction
Applications create data with insertOne and insertMany, but teams also bulk-load fixtures from JSON/CSV and backup production with dump tools. This chapter covers insert options, ordered batches, write concern basics, and mongoimport / mongoexport / mongodump / mongorestore.
Prerequisites
- BSON and Document Basics
- Installation and mongosh
- Sample database
hello_demowith a few documents
insertOne and insertMany
Return value includes insertedId or insertedIds map.
Ordered vs Unordered insertMany
Default ordered: true stops on first error:
db.posts.insertMany(
[
{ _id: 1, title: "One" },
{ _id: 1, title: "Duplicate" },
{ _id: 2, title: "Two" }
],
{ ordered: true }
)
// Second doc fails; third never runsordered: false continues after errors (useful for bulk ingest):
db.posts.insertMany(
[
{ _id: 10, title: "Ten" },
{ _id: 10, title: "Dup" },
{ _id: 11, title: "Eleven" }
],
{ ordered: false }
)
// Doc 11 still inserts if 10 duplicatesWrite Concern (Concept)
Write concern answers: "When does MongoDB acknowledge a write?"
| Level | Meaning |
|---|---|
{ w: 1 } | Primary acknowledged (default on standalone) |
{ w: "majority" } | Majority of replica set members (production) |
{ j: true } | Journal flushed to disk |
db.posts.insertOne(
{ title: "Important" },
{ writeConcern: { w: "majority", wtimeout: 5000 } }
)Replica sets — chapter 15. Standalone dev uses defaults.
Insert Failures
| Error | Cause |
|---|---|
| E11000 duplicate key | Duplicate _id or unique index value |
| Document too large | Exceeds 16 MB |
| Validation error | Schema validator rejects document — chapter 14 |
mongoexport (JSON / CSV)
Export collection to file:
Use exports for fixtures, analytics handoff, or migration between environments.
mongoimport
posts.json (JSON array format):
[
{ "title": "Imported", "slug": "imported", "published": true },
{ "title": "Also Imported", "slug": "also-imported", "published": false }
]mongoimport --uri="mongodb://127.0.0.1:27017" \
--db=hello_demo \
--collection=posts \
--file=posts.json \
--jsonArrayOne JSON object per line (NDJSON):
mongoimport --uri="mongodb://127.0.0.1:27017" \
--db=hello_demo \
--collection=posts \
--file=posts.ndjsonWarning
Import and _id
Imported documents with existing _id values collide on re-import. Drop collection or use --mode=upsert with --upsertFields for idempotent loads.
mongodump and mongorestore (BSON Backup)
Logical backup at database/collection level:
# Dump entire database to folder
mongodump --uri="mongodb://127.0.0.1:27017" --db=hello_demo --out=./backup
# Restore into same or another server
mongorestore --uri="mongodb://127.0.0.1:27017" --db=hello_demo ./backup/hello_demoCode explanation:
mongodumppreserves BSON types (dates, ObjectId)—better than JSON for full fidelity- Atlas provides automated snapshots—Atlas chapter
Seed Script Pattern
scripts/seed_posts.js:
mongosh hello_demo scripts/seed_posts.jsPost-Chapter Checklist
- You used
insertOneandinsertManywithorderedoption - You exported with
mongoexportand imported withmongoimport - You ran
mongodump/mongorestoreonce in dev - You understand duplicate
_iderrors
FAQ
insert vs save?
Legacy save() upserts by _id. Prefer explicit insertOne / updateOne with upsert — chapter 7.
Can mongoimport create indexes?
No—create indexes after import — Indexes.
Large dataset import speed?
Use ordered: false, disable unnecessary indexes during import, increase bulk batch size in drivers.
Backup while app is running?
mongodump is consistent for single replica set; use Atlas snapshots or ops-runbook for sharded clusters.
JSON vs BSON backup?
JSON is human-readable; BSON backup via mongodump preserves all types exactly.