Labs ICT
Pro Login

Insert Documents

Adding data to your collections.

Inserting Documents in MongoDB

So you want to learn how to put data into MongoDB? Think of MongoDB collections like a box. You've got this empty box, and now you're tossing documents into it. That's essentially what inserting is all about. No complicated SQL INSERT statements here—just drop your JavaScript objects in and MongoDB handles the rest.

The two main methods you'll use are insertOne() and insertMany(). insertOne() is for when you want to add a single document, while insertMany() lets you add multiple documents in one go. Both are beautifully simple, and that's one of the things that makes MongoDB so developer-friendly.

Here's the cool part: every document you insert gets an _id field automatically if you don't provide one. MongoDB generates a unique ObjectId for it, so you never have to worry about managing primary keys yourself. It's like MongoDB says, "I got you, buddy."

Insert One Document

Let's start with insertOne(). You pass it a single JavaScript object, and boom—it's in your collection. This is your go-to when you're adding one record at a time, like creating a new user account or saving a single blog post.

Trust me, once you see how clean this is, you'll wonder why databases ever had to be so complicated. The method returns an acknowledgment object that tells you if the insert was successful and the _id of the new document.

db.users.insertOne({
  name: "Alice Johnson",
  email: "alice@example.com",
  age: 28,
  hobbies: ["reading", "hiking"]
})
Try it Yourself →

Insert Many Documents

Now, when you've got a bunch of documents to add, insertMany() is your best friend. Instead of calling insertOne() twenty times (please don't), you just pass an array of objects and MongoDB adds them all at once. It's efficient, it's clean, and it's what you should be doing for bulk inserts.

One thing to keep in mind: by default, insertMany() performs an ordered insert. That means if one document fails, the rest won't be inserted. If you want to insert as many as possible regardless of failures, you can set ordered to false. We'll cover that more in the bulk operations section.

db.users.insertMany([
  { name: "Bob Smith", email: "bob@example.com", age: 35 },
  { name: "Carol White", email: "carol@example.com", age: 42 },
  { name: "Dave Brown", email: "dave@example.com", age: 31 }
])
Try it Yourself →

Nested Documents

MongoDB isn't limited to flat key-value pairs. You can nest objects inside objects, arrays inside objects, objects inside arrays—you get the idea. This is one of MongoDB's superpowers. Instead of spreading data across multiple tables like in SQL, you can keep related data together in one document.

Think of it like this: in SQL, you'd need a separate addresses table linked by a foreign key. In MongoDB, you just embed the address right inside the user document. It's like putting all your shipping info right on the package instead of in a separate filing cabinet.

db.users.insertOne({
  name: "Eve Davis",
  email: "eve@example.com",
  address: {
    street: "123 Main St",
    city: "Springfield",
    state: "IL",
    zip: "62701"
  },
  orders: [
    { item: "laptop", price: 999.99, date: new Date() },
    { item: "mouse", price: 29.99, date: new Date() }
  ]
})
Try it Yourself →

🧪 Quick Quiz

What is the _id field?