InsertMany
InsertMany: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 12 of 100
InsertMany
Foundations & CRUD → Queries & Schema → Aggregation & Scale → Atlas & Projects
Foundations & CRUD · 1 — Documents · ~6 min · MongoDB — CRUD Operations
What is this?
insertMany writes an array of documents in one call. You can control ordered behavior — stop on first error, or continue inserting the rest.
Why should you care?
Seeding a product catalog, importing CSV batches, or bulk-loading sensor history is much faster than a loop of insertOne from the client.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
db.products.insertMany([
{ sku: "P1", name: "USB-C Hub", price: 1499, stock: 40 },
{ sku: "P2", name: "Webcam", price: 2999, stock: 15 },
{ sku: "P3", name: "Ring Light", price: 999, stock: 80 }
], { ordered: true })
db.products.find({ sku: { $in: ["P1", "P2", "P3"] } })
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- Three product documents are inserted together.
- ordered: true stops if one fails.
- $in finds all three SKUs.
- insertMany returns insertedIds for each.
Practice next
- Run insertMany with the three products.
- Count with countDocuments({ sku: /^P/ }).
- Try insertMany with a duplicate sku if you add a unique index (later).
- Add a fourth product in the array.
- Log the length of result.insertedIds.
Remember
insertMany bulk-creates documents. ordered controls fail-fast vs continue. Great for seeds and imports.
Myntra seasonal catalog load
Before a sale, merchandising imports 5,000 new SKUs from a feed into products via batched insertMany.
Outcome: Catalog goes live in minutes instead of hours of single inserts.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!