liveBook · Manning

insertMany()

As explained in MongoDB 8.0 in Action, Third Edition

insertMany() is a MongoDB write method that inserts multiple documents into a collection in one operation.

insertMany() adds multiple documents to a MongoDB collection in one database operation, avoiding a separate round trip for each document. section 4.2

Use insertMany() with these practical constraints and options:

  • Pass an array of documents, such as several route records, to insert them together. section 4.2
  • Keep the combined documents below MongoDB’s 16 MB BSON document-size limit. section 4.2
  • Use ordered processing when later inserts should stop after an earlier failure; unordered processing continues with subsequent documents and may improve performance. section 4.2
  • Read acknowledged and insertedIds from the response to confirm execution and identify inserted documents. section 4.2

Bulk-inserting route documents

Run db.routes.insertMany([...]) with route documents for American Airlines, British Airways, and Air France. A successful response reports acknowledged: true and an insertedIds entry for each document. section 4.2

Listing 4.2 Inserting many documents
[primary] sample_training> db.routes.insertMany([
  {
    airline: { id: 413, name: 'American Airlines', alias: 'AA',
➥iata: 'AAL' },
    src_airport: 'DFW',
    dst_airport: 'LAX',
    codeshare: '',
    stops: 0,
    airplane: '737'
  },
  {
    airline: { id: 411, name: 'British Airways', alias: 'BA', 
➥iata: 'BAW' },
    src_airport: 'LHR',
    dst_airport: 'SFO',
    codeshare: 'Y',
    stops: 0,
    airplane: '747'
  },
  {
    airline: { id: 412, name: 'Air France', alias: 'AF', iata: 'AFR' },
    src_airport: 'CDG',
    dst_airport: 'JFK',
    codeshare: '',
    stops: 0,
    airplane: '777'
  }
])

{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId('661a252acf6203ef2a1db277'),
    '1': ObjectId('661a252acf6203ef2a1db278'),
    '2': ObjectId('661a252acf6203ef2a1db279')
  }}
[primary] sample_training>

Inspect the complete shell listing for inserting three route documents and reading their generated identifiers. section 4.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains array input, acknowledgement, identifiers, size limits, ordering, and failure behavior without covering deeper write internals.
Examples
Demonstrates three airline route documents inserted into the routes collection with acknowledged status and generated ObjectIds.
What it assumes
Assumes familiarity with MongoDB collections and documents while demonstrating the required array-of-documents input from a basic insertion comparison.
What you can do after
Operate MongoDB collection inserts in bulk and interpret acknowledgement, identifiers, ordering, and document-size constraints.
How it connects
Connects insertMany() with insertOne(), CRUD operations, document identifiers, ordered writes, and bulkWrite.

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.