insertMany() (Bulk Insert)

Last updated: 2026-10-09

insertMany() (Bulk Insert)

insertMany() inserts multiple documents in one call, e.g. `db.users.insertMany([{ name: 'A' }, { name: 'B' }])`. With ordered: true (default) inserts proceed in order and stop at the first error; with false, the remaining documents are still attempted. The result contains acknowledged and insertedIds, mapping each document index to its _id. It saves many round trips compared to looping insertOne(), making it the go-to for seeding and migrations.

CategoryWrite
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

db.<collection>.insertMany(<documents>, { ordered: <bool>, writeConcern: <options> })

⚙️ Parameters

documentsArray of BSON documents to insert.
orderedOptional. true stops at the first error, false skips it and continues; default true.
writeConcernOptional. Write concern.

Returns:Result object with acknowledged and insertedIds (index-to-_id mapping).

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// insertMany: insert three users at once
db.users.insertMany([
  { name: 'Alice', age: 21 },
  { name: 'Bob', age: 25 },
  { name: 'Cara', age: 30 }
])

Output:

{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId('64c8a1b20000000000000001'),
    '1': ObjectId('64c8a1b20000000000000002'),
    '2': ObjectId('64c8a1b20000000000000003')
  }
}

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhat is the difference between insertMany and insertOne?
AinsertMany() writes many documents in a single round trip; insertOne() writes one. For bulk workloads insertMany() far outperforms looping insertOne().
QWhat does the ordered option of insertMany do?
AWith ordered: true, inserts stop at the first error and the batch rolls back; with false, failing documents are skipped and the rest still insert.
QWhat does insertMany return when only some documents fail?
AIt returns acknowledged plus insertedIds covering only the successfully inserted indexes; failure details live in the thrown BulkWriteError.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏