Skip to main content

How do you create several documents at once?

To create several documents at once in MongoDB, use the insertMany() method.

Example (Mongo Shell / JavaScript syntax):

js
db.users.insertMany([ { name: "Alex", age: 25 }, { name: "Maria", age: 30 }, { name: "John", age: 20 } ])

What insertMany does:

  • takes an array of documents
  • inserts all the records in a single operation
  • returns information about the inserted _ids

When to use it:

  • when several documents need to be added at once (e.g. a test dataset)

So insertMany() is what's used for bulk document creation in MongoDB.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.