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.
| Category | Write |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.insertMany(<documents>, { ordered: <bool>, writeConcern: <options> })
⚙️ Parameters
| documents | Array of BSON documents to insert. |
|---|---|
| ordered | Optional. true stops at the first error, false skips it and continues; default true. |
| writeConcern | Optional. 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
- With ordered: true, one failure rolls back the documents already inserted in that batch
- insertedIds keys are document indexes in the array ('0', '1', ...)
- A single batch must stay within maxWriteBatchSize and the 16MB limit
🏷️ Related Tags
💡 Related Cases
Documents and BSON: The Cornerstone of MongoDB Data
Inserting Documents: A Detailed Explanation of `insertOne` and `insertMany`
📚 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.