A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/mongodb/mongodb-insert-multiple-document-using-mongoshell/ below:

MongoDB - Insert Multiple Document Using MongoShell

MongoDB - Insert Multiple Document Using MongoShell

Last Updated : 10 Jun, 2025

MongoDB is a scalable and flexible NoSQL database that stores data in JSON like documents. Inserting multiple documents in a batch is much faster in MongoDB than inserting them individually.

Irrespective of whether you are working with batch processing operations or big data, knowing how to insert more than one document efficiently is crucial. We will detail the steps involved in inserting multiple documents into a MongoDB collection from the Mongo shell in this article.

Why Use insertMany() for Bulk Insert Operations?

The insertMany() is a mongo shell method, which can insert multiple documents. This method can be used in the multi-document transactions, which is significantly faster than callinginsertOne()repeatedly. In this method, we can add documents in the collection with or without _id field. If you add documents without _id field, then mongodb will automatically add _id fields for each documents and assign them with a unique ObjectId. This method by default insert documents in order.

Syntax:

db.collection.insertMany(
[ <Document1>, <Document2>, <Document3> ... ],
{
writeConcern: <Document>,
order: <Boolean>
}
)

Parameters

Return

When you use insertMany(), MongoDB returns an object containing:

Examples: Inserting Multiple Documents into the student Collection

Assume you're working with a collection called studentin the GeeksforGeeksdatabase. Here’s how to insert multiple documents at once:

Database: GeeksforGeeks
Collection: student
1. Inserting Multiple Documents Without Custom _id field

In this example, we are inserting multiple documents in the student collection without _id field db.collection.insertManymethod. Here, three documents are inserted at once. MongoDB will automatically generate the _id for each document.

2. Inserting Multiple Documents with Custom _id

In this example, we are inserting multiple documents in the student collection with _id field db.collection.insertMany() method. Here, we manually assign the _id for each document, ensuring uniqueness across all inserted documents.

3. Inserting Documents with Mixed Data Types

MongoDB allows you to insert documents with varying data types. This flexibility is useful when working with diverse data sets. This example demonstrates inserting documents where fields have different data types, including arrays and booleans.

db.student.insertMany([
{ name: "Alice", age: 22, courses: ["Computer Science", "Mathematics"], active: true },
{ name: "Bob", age: 24, courses: ["Mechanical Engineering", "Physics"], active: false }
])

This example demonstrates inserting documents where fields have different data types, including arrays and booleans.

Handling Errors and Failures in insertMany() Example of inserting with ordered: false:
db.student.insertMany([
{ _id: 101, name: "Alice", age: 22, course: "Computer Science" },
{ _id: 101, name: "Bob", age: 24, course: "Mechanical Engineering" } // Duplicate _id
], { ordered: false })

In this example, MongoDB will insert the first document successfully and skip the second one due to the duplicate _id.

Best Practices for Inserting Multiple Documents
  1. Limit the Batch Size: For very large datasets, break the insert operation into smaller batches to prevent overwhelming the system and ensure better performance.
  2. Use ordered: false When Necessary: When you don't mind skipping over errors and want to continue the insertion process, use the ordered: false option.
  3. Verify for Duplicates: Prior to inserting, verify for possible duplicate entries by using MongoDB's query mechanism in order not to make unnecessary errors.
  4. Use Bulk Operations for Performance: For mass inserts, use bulk operations such as bulkWrite() for improved performance and greater control over how insertions occur.
Conclusion

Inserting several documents simultaneously using theinsertMany()method is a critical skill when dealing with MongoDB. It offers a fast and scalable means of inserting lots of data into your collections. With best practice and knowledge of the syntax, you can carry out insert operations smoothly and at high speed. Whether you're managing a small project or dealing with large datasets, mastering insertMany() will significantly enhance your MongoDB workflow



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4