MongoDB Insert Documents
To add data to MongoDB, you insert documents into a collection with insertOne() or insertMany().
insertOne()
Example
use institute
db.students.insertOne({
name: "Ali Khan",
city: "Karachi",
age: 19,
marks: 78,
course: "Web Development",
skills: ["HTML", "CSS"]
}){
acknowledged: true,
insertedId: ObjectId('6710a1f2c3b4d5e6f7a8b901')
}acknowledged: truemeans the server saved itinsertedIdis the_idMongoDB created for the new document
insertMany()
Pass an array of documents to insert several at once:
Example
db.students.insertMany([
{ name: "Ayesha Siddiqui", city: "Lahore", age: 21, marks: 85,
course: "Python Programming", skills: ["Python"] },
{ name: "Bilal Ahmed", city: "Karachi", age: 22, marks: 64,
course: "Web Development", skills: ["HTML", "JavaScript"] },
{ name: "Fatima Noor", city: "Islamabad", age: 20, marks: 92,
course: "Graphic Designing", skills: ["Photoshop", "Illustrator"] },
{ name: "Hamza Raza", city: "Karachi", age: 18, marks: 70 },
{ name: "Sana Iqbal", city: "Lahore", age: 23, marks: 88,
course: "Python Programming", skills: ["Python", "SQL"] }
]){
acknowledged: true,
insertedIds: {
'0': ObjectId('6710a1f2c3b4d5e6f7a8b902'),
'1': ObjectId('6710a1f2c3b4d5e6f7a8b903'),
'2': ObjectId('6710a1f2c3b4d5e6f7a8b904'),
'3': ObjectId('6710a1f2c3b4d5e6f7a8b905'),
'4': ObjectId('6710a1f2c3b4d5e6f7a8b906')
}
}The collection now has the six students used in the rest of this tutorial. Your ObjectId values will be different.
If you followed the last lesson, first remove the test document with db.students.deleteOne({ name: "Test Student" }), so your results match the examples.
The _id Field
Every document must have a unique _id.
- If you leave it out, MongoDB adds an ObjectId, a 12-byte value that includes the time it was created
- You may set your own
_id, such as a roll number, as long as it is unique
Example
db.courses.insertOne({ _id: "WEB", title: "Web Development", months: 6 }){ acknowledged: true, insertedId: 'WEB' }Inserting another document with _id: "WEB" fails:
MongoServerError: E11000 duplicate key error collection: institute.courses index: _id_ dup key: { _id: "WEB" }Value Types
| Type | Example |
|---|---|
| String | "Karachi" |
| Number | 19, 78.5 |
| Boolean | true, false |
| Array | ["HTML", "CSS"] |
| Embedded document | { phone: "0300-1112233" } |
| Date | new Date("2026-10-15") |
| Null | null |
| ObjectId | ObjectId('6710a1f2c3b4d5e6f7a8b901') |
| Decimal | NumberDecimal("2450.50") for exact money values |
Nested Data and Dates
A course document can hold its own list of modules, each one a small document:
Example
db.courses.insertOne({
_id: "PY",
title: "Python Programming",
months: 4,
startDate: new Date("2026-11-02"),
modules: [
{ week: 1, topic: "Variables and types" },
{ week: 2, topic: "Loops and functions" }
]
}){ acknowledged: true, insertedId: 'PY' }This is called embedding. Data that is always read together lives in one document, so no join is needed.
Ordered and Unordered Inserts
By default, insertMany stops at the first error. Add { ordered: false } to insert every valid document and report the failed ones at the end:
Example
db.courses.insertMany(
[ { _id: "GD", title: "Graphic Designing" }, { _id: "WEB", title: "Duplicate" } ],
{ ordered: false }
)Here GD is saved, and only the duplicate WEB fails.
Exercise
Exercise
What happens if you insert a document without an _id field?
