We have just used .insertOne() to insert a single document into a collection, but what if we want to insert multiple documents into a collection? Here’s where the MongoDB .insertMany() method comes in.

As its name suggests, .insertMany() will insert multiple documents into a collection. Much like .insertOne(), if the collection we’ve specified does not exist, one will be created.

The syntax for the method is as follows:

db.<collection>.insertMany( [<document1>, <document2>, ...], { writeConcern: <document>, ordered: <boolean> } );

This method has three parameters:

  1. An array of documents; the documents we want to add to the collection.
  2. A parameter named writeConcern.
  3. A parameter named ordered.

The ordered parameter can be handy since it allows us to specify if MongoDB should perform an ordered or unordered insert. If set to false, documents are inserted in an unordered format. If set to true, the default behavior, MongoDB will insert the documents in the order given in the array.

It’s worth noting that with ordered inserts, if a single document fails to be inserted, the entire insert operation will cease, and any remaining documents will not be inserted. On the other hand, unordered inserts will continue in the case of an insert failure and attempt to insert any remaining documents.

Let’s look at an example of .insertMany() on a collection named students:

db.students.insertMany([ { name: "Mia Ramirez", age: 15 }, { name: "Kiv Huang", age: 17 }, { name: "Sam Soto", age: 16 } ], { ordered: true })

The command above will insert the documents in the order specified. Below you will find the output of the command with an additional note that indicates which _id represents which document.

{ acknowledged: true, insertedIds: { '0': ObjectId("63054a5376742c0e5a0cfafb"), // Mia '1': ObjectId("63054a5376742c0e5a0cfafc"), // Kiv '2': ObjectId("63054a5376742c0e5a0cfafd") // Sam } }

Let’s practice adding multiple new restaurants to our listingsAndReviews collection!



Connect to the restaurants database. Insert two new documents in an ordered fashion into the listingsAndReviews collection. The documents should have the following properties, respectively:

// 1st document name: "Boucherie", borough: "Manhattan", cuisine: "American", restaurant_id: "49246215" // 2nd document name: "Carmines", borough: "Manhattan", cuisine: "Italian", restaurant_id: "48259401"

Using .findOne() method, query for one of the documents that you just inserted using the name field.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?