Docs Menu

Insert Multiple Documents

Note

If you specify a callback method, insertMany() returns nothing. If you do not specify one, this method returns a Promise that resolves to the result object when it completes. See our guide on Promises and Callbacks for more information, or the API documentation for information on the result object.

You can insert multiple documents using the collection.insertMany() method. The insertMany() takes an array of documents to insert into the specified collection.

You can specify additional options in the options object passed as the second parameter of the insertMany() method. Specify ordered:true to prevent inserting the remaining documents if the insertion failed for a previous document in the array.

Specifying incorrect parameters for your insertMany() operation can cause problems. Attempting to insert a field to a value that would violate unique index rules will throw a duplicate key error.

Note

You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.

If you run the preceding example, you should see the following output:

3 documents were inserted
←  Insert a DocumentUpdate & Replace Operations →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.