liveBook · Manning

Bulk insertion

As explained in MongoDB 8.0 in Action, Third Edition

Bulk insertion is the technique of inserting multiple records through a grouped database operation rather than separate operations.

Bulk insertion inserts multiple records through one grouped database operation instead of separate requests. In MongoDB, insertMany() accepts an array of documents and reduces database round trips. section 4.2

Use insertMany() with these operational considerations:

  • Keep the combined documents below MongoDB’s 16 MB BSON document-size limit. section 4.2
  • Use ordered: true when a failure should stop subsequent insertions; use ordered: false to continue after failures, without relying on insertion order. section 4.2
  • Inspect error indexes to identify failed array elements; unordered operations may report multiple failures. section 4.2

Insert several route documents

db.routes.insertMany([{ airline: { id: 413, name: 'American Airlines' }, src_airport: 'DFW', dst_airport: 'LAX' }, { airline: { id: 411, name: 'British Airways' }, src_airport: 'LHR', dst_airport: 'SFO' }]) returns acknowledged: true and an insertedIds entry for each document.

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains array input, acknowledgement, size limits, ordering, continuation, and failure indexes, but not transaction semantics.
Examples
Demonstrates inserting airline route documents into the routes collection and reading acknowledged results with inserted identifiers.
What it assumes
Assumes single-document insertion and MongoDB collection usage, then extends them to array-based insertion.
What you can do after
Operate/configure insertMany() for grouped inserts and diagnose failed documents from response indexes.
How it connects
Connects bulk insertion with insertMany(), BSON, ObjectId, ordered operations, and error handling.

Also explained in

Compare how 2 books explain bulk insertion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.