Insert Operations: insertOne, insertMany & Write Concerns0%

Insert Operations: insertOne, insertMany & Write Concerns

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Insert Operations: insertOne, insertMany & Write Concerns

Writing documents into MongoDB is the initial step in any data lifecycle. MongoDB provides high-throughput insert methods with granular control over batch processing, ordered execution, and Write Concerns.


1. Inserting a Single Document: insertOne()

The insertOne() method inserts a single document into a collection. If the document does not contain a top-level _id field, the driver generates an ObjectId automatically.

JavaScript
// Inserting into 'courses' collection
const result = await db.collection('courses').insertOne({
title: 'Full-Stack MERN Mastery',
slug: 'full-stack-mern-mastery',
price: 4999,
category: 'Web Development',
isPublished: true,
tags: ['react', 'node', 'mongodb', 'express'],
instructor: {
name: 'Sumit Sharma',
experienceYears: 10
},
createdAt: new Date()
});
 
console.log('Inserted ID:', result.insertedId);
console.log('Acknowledged:', result.acknowledged); // true

2. Inserting Multiple Documents: insertMany()

To insert multiple records in a single network round-trip, pass an array of documents to insertMany():

JavaScript
const students = [
{ name: 'Aarav Patel', email: 'aarav@example.com', enrolledAt: new Date() },
{ name: 'Priya Sharma', email: 'priya@example.com', enrolledAt: new Date() },
{ name: 'Rohan Gupta', email: 'rohan@example.com', enrolledAt: new Date() }
];
 
const batchResult = await db.collection('students').insertMany(students, {
ordered: true // Default: stops on first error. Set to false for unordered inserts.
});
 
console.log('Inserted Count:', batchResult.insertedCount);
console.log('Inserted IDs:', batchResult.insertedIds);

Ordered vs Unordered Inserts:

  • ordered: true (Default): MongoDB processes documents sequentially. If document 2 fails (e.g. duplicate key), the operation halts immediately. Documents after 2 are not inserted.
  • ordered: false: MongoDB attempts to insert all documents in parallel. If document 2 fails, MongoDB reports the error but continues inserting documents 3, 4, etc.

3. Understanding Write Concerns (w)

A Write Concern describes the level of acknowledgment requested from MongoDB for write operations:

  • w: 1 (Acknowledged): The Primary node acknowledges the write has been written to memory/journal. Fast, standard default.
  • w: "majority": The write must be committed by a majority of replica set members before returning success. Protects against data loss during sudden primary failover.
  • j: true (Journaled): Guarantees the write has been written to the on-disk journal before responding.
JavaScript
// High-security financial transaction write concern:
await db.collection('transactions').insertOne(
{ accountId: 'acc_01', amount: 50000, type: 'CREDIT' },
{ writeConcern: { w: 'majority', j: true, wtimeoutMS: 5000 } }
);

Interactive Quiz

Multiple Choice Questions

Question 1
What happens if you insert a document into a MongoDB collection without providing an _id field?
Question 2
In collection.insertMany(docs, { ordered: true }), what occurs if the 3rd document violates a unique index constraint?
Question 3
How do you instruct insertMany() to continue inserting remaining documents even if one document in the batch fails?
Question 4
What does the write concern option { w: 'majority' } guarantee?
Question 5
What property on the result object of collection.insertOne() contains the generated primary key?

Next Lesson

Find Queries & Comparison Operators ($eq, $gt, $in, $nin)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext