insertMany() — MongoDB Technical Interview Questions & Answers
Inserts multiple documents in one call.
insertMany()
- Simple meaning: insertMany adds an array of documents in one call.
- How to use: db.students.insertMany([ { name: "Asha", roll: 12 }, { name: "Kabir", roll: 13 } ]).
- ordered: true (default) stops at the first error. ordered: false keeps inserting the rest.
- Try this: two students, then find().count() or find().
- Common mistake: insertMany({ name: "Asha" }) without an array.
- Interview tip: Array of docs. Mention ordered vs unordered.
Technical Interview Tip: insertMany([ {}, {} ]).
Code Examples for insertMany()
insertMany() — working example
db.users.insertMany([
{ name: "Ada", email: "ada@example.com", age: 36, city: "Pune", skills: ["Java", "MongoDB"] },
{ name: "Lin", email: "lin@example.com", age: 22, city: "Delhi", skills: ["CSS", "JS"] }
])insertMany() — insertMany inserts an array of documents in one call; each gets an _id if missing.
How to Explain insertMany() to an Interviewer
insertOne vs insertMany?
One object vs array of objects