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

insertOne and insertMany

Return value includes insertedId or insertedIds map.

Ordered vs Unordered insertMany

Default ordered: true stops on first error:

javascript
db.posts.insertMany(
  [
    { _id: 1, title: "One" },
    { _id: 1, title: "Duplicate" },
    { _id: 2, title: "Two" }
  ],
  { ordered: true }
)
// Second doc fails; third never runs

ordered: false continues after errors (useful for bulk ingest):

javascript
db.posts.insertMany(
  [
    { _id: 10, title: "Ten" },
    { _id: 10, title: "Dup" },
    { _id: 11, title: "Eleven" }
  ],
  { ordered: false }
)
// Doc 11 still inserts if 10 duplicates

Write Concern (Concept)

Write concern answers: "When does MongoDB acknowledge a write?"

LevelMeaning
{ w: 1 }Primary acknowledged (default on standalone)
{ w: "majority" }Majority of replica set members (production)
{ j: true }Journal flushed to disk
javascript
db.posts.insertOne(
  { title: "Important" },
  { writeConcern: { w: "majority", wtimeout: 5000 } }
)

Replica sets — chapter 15. Standalone dev uses defaults.

Insert Failures

ErrorCause
E11000 duplicate keyDuplicate _id or unique index value
Document too largeExceeds 16 MB
Validation errorSchema 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):

json
[
  { "title": "Imported", "slug": "imported", "published": true },
  { "title": "Also Imported", "slug": "also-imported", "published": false }
]
bash
mongoimport --uri="mongodb://127.0.0.1:27017" \
  --db=hello_demo \
  --collection=posts \
  --file=posts.json \
  --jsonArray

One JSON object per line (NDJSON):

bash
mongoimport --uri="mongodb://127.0.0.1:27017" \
  --db=hello_demo \
  --collection=posts \
  --file=posts.ndjson

Warning

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:

bash
# 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_demo

Code explanation:

  • mongodump preserves BSON types (dates, ObjectId)—better than JSON for full fidelity
  • Atlas provides automated snapshots—Atlas chapter

Seed Script Pattern

scripts/seed_posts.js:

bash
mongosh hello_demo scripts/seed_posts.js

Post-Chapter Checklist

  • You used insertOne and insertMany with ordered option
  • You exported with mongoexport and imported with mongoimport
  • You ran mongodump / mongorestore once in dev
  • You understand duplicate _id errors

FAQ

insert vs save?

Legacy save() upserts by _id. Prefer explicit insertOne / updateOne with upsertchapter 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.