MongoDB Tutorial menu

MongoDB Insert Documents

To add data to MongoDB, you insert documents into a collection with insertOne() or insertMany().

insertOne()

Example

javascript
use institute

db.students.insertOne({
  name: "Ali Khan",
  city: "Karachi",
  age: 19,
  marks: 78,
  course: "Web Development",
  skills: ["HTML", "CSS"]
})
Output
{
  acknowledged: true,
  insertedId: ObjectId('6710a1f2c3b4d5e6f7a8b901')
}
  • acknowledged: true means the server saved it
  • insertedId is the _id MongoDB created for the new document

insertMany()

Pass an array of documents to insert several at once:

Example

javascript
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"] }
])
Output
{
  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

javascript
db.courses.insertOne({ _id: "WEB", title: "Web Development", months: 6 })
Output
{ acknowledged: true, insertedId: 'WEB' }

Inserting another document with _id: "WEB" fails:

Output
MongoServerError: E11000 duplicate key error collection: institute.courses index: _id_ dup key: { _id: "WEB" }

Value Types

TypeExample
String"Karachi"
Number19, 78.5
Booleantrue, false
Array["HTML", "CSS"]
Embedded document{ phone: "0300-1112233" }
Datenew Date("2026-10-15")
Nullnull
ObjectIdObjectId('6710a1f2c3b4d5e6f7a8b901')
DecimalNumberDecimal("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

javascript
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" }
  ]
})
Output
{ 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

javascript
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?

Learn MongoDB with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp