InsertOne vs insertMany in MongoDB: Key Differences and Usage
insertOne inserts a single document into a collection, while insertMany inserts multiple documents at once. Use insertOne for single inserts and insertMany to efficiently add many documents in one operation.Quick Comparison
Here is a quick side-by-side comparison of insertOne and insertMany in MongoDB.
| Factor | insertOne | insertMany |
|---|---|---|
| Number of documents inserted | One document | Multiple documents (array) |
| Operation type | Single insert operation | Bulk insert operation |
| Return value | Result with insertedId | Result with insertedIds object |
| Performance | Slower for many inserts | Faster for batch inserts |
| Error handling | Error on single failure | Can continue or stop on errors |
| Use case | Insert one record | Insert many records at once |
Key Differences
insertOne is designed to add exactly one document to a MongoDB collection. It returns a result containing the insertedId of the new document, making it easy to track that single insert. This method is simple and straightforward for cases where you only need to add one record.
On the other hand, insertMany accepts an array of documents and inserts them all in one operation. It returns an object with insertedIds, an object mapping indexes to the IDs for each inserted document. This bulk operation is more efficient when adding many documents because it reduces the number of round trips to the database.
Another difference is error handling: insertOne fails if the single insert fails, while insertMany can be configured with options like ordered to control whether it stops on the first error or continues inserting the rest.
Code Comparison
Example of inserting a single document using insertOne:
const { MongoClient } = require('mongodb'); async function runInsertOne() { const client = new MongoClient('mongodb://localhost:27017'); await client.connect(); const db = client.db('testdb'); const collection = db.collection('users'); const result = await collection.insertOne({ name: 'Alice', age: 25 }); console.log('Inserted document ID:', result.insertedId); await client.close(); } runInsertOne();
insertMany Equivalent
Example of inserting multiple documents using insertMany:
const { MongoClient } = require('mongodb'); async function runInsertMany() { const client = new MongoClient('mongodb://localhost:27017'); await client.connect(); const db = client.db('testdb'); const collection = db.collection('users'); const docs = [ { name: 'Bob', age: 30 }, { name: 'Carol', age: 27 }, { name: 'Dave', age: 22 } ]; const result = await collection.insertMany(docs); console.log('Inserted document IDs:', result.insertedIds); await client.close(); } runInsertMany();
When to Use Which
Choose insertOne when you need to add a single document and want simple, direct insertion with immediate feedback on that one record. It is ideal for operations like adding a new user or logging a single event.
Choose insertMany when you have multiple documents to add at once, such as importing data or batch processing. It is more efficient and faster because it reduces the number of database calls and can handle errors flexibly.
Key Takeaways
insertOne for inserting a single document with simple feedback.insertMany to insert multiple documents efficiently in one operation.insertMany returns an object of inserted IDs, while insertOne returns a single inserted ID.insertMany can be configured to continue inserting even if some documents fail.