MongoDB Tutorial 0/120 lessons ~6 min read Lesson 31
Insert Documents
Inserting is how data enters a MongoDB collection.
Course progress0%
Focus
6 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Inserting is how data enters a MongoDB collection. The two workhorses are insertOne and insertMany. Both return acknowledgements with the generated _ids, and both are atomic per document.
Understanding the topic
insertOne(doc)— single document, returns inserted_id.insertMany([...])— batch insert, ordered by default.- Pass
{ ordered: false }to continue on errors. - Server generates
ObjectIdif no_idsupplied. - Inserts respect schema validators and indexes (including unique).
Syntax reference
js
db.users.insertOne({ name: "Ada", email: "ada@x.com" });db.products.insertMany([{ sku: "A1", price: 9.99 },{ sku: "A2", price: 19.99 }], { ordered: false });
Real-world use
Batch ETL jobs use insertMany({ ordered: false }) to skip duplicates without aborting the whole batch.
Best practices
- Batch inserts when possible — fewer round-trips.
- Use
ordered: falsefor resilient bulk loads.
Common mistakes
- Re-inserting an existing
_idthrows — useupsertinstead.
Ready to mark this lesson complete?Track your journey across the entire course.