Chapter 6: MongoDB mongosh Insert

MongoDB mongosh Insert (aka How to add data using insertOne() and insertMany() in the mongosh shell)

This is the C in CRUDCreate / Insert operations. It’s how you bring new documents (your data records) into a collection.

1. The Two Main Insert Methods in mongosh (2026 Reality)

Method What it does How many documents? Returns (on success) Most common use case
db.collection.insertOne(doc) Inserts exactly one document 1 { acknowledged: true, insertedId: ObjectId(…) } Adding single items (user signup, new post, log entry)
db.collection.insertMany([doc1, doc2, …]) Inserts multiple documents in one call Many { acknowledged: true, insertedIds: { “0”: ObjectId(…), “1”: … } } Bulk import, seeding data, adding many products/orders

Important notes (teacher yelling gently):

  • If the collection doesn’t exist → MongoDB creates it automatically.
  • If the database doesn’t exist → it gets created too (as we learned earlier).
  • Every document must have (or will get) a unique _id field.
    • You can provide your own _id (string, number, etc.)
    • Or let MongoDB auto-generate an ObjectId (most common & recommended).

Old method warning: db.collection.insert() (single doc or array) is deprecated since MongoDB 3.x / 4.x era. In 2026 you should always prefer insertOne() or insertMany().

2. Let’s Do It Hands-on — Step by Step

Assume you’re already in mongosh and switched to a database:

JavaScript

Example 1: insertOne() — Single Document (The Hello World of Inserts)

JavaScript

What you see back (typical output):

JSON

→ Success! One document added. → _id was auto-generated because we didn’t provide one.

Now verify:

JavaScript

You should see something like:

JSON

Example 2: insertMany() — Multiple Documents at Once

JavaScript

Typical output:

JSON

→ Three new documents added in one round-trip to the server (efficient!).

Check them:

JavaScript

You’ll see all five wishes now (the one from before + these three).

Example 3: Providing Your Own _id (When You Want Control)

JavaScript

→ Useful when importing from another system or using natural keys (product codes, usernames, invoice numbers).

Warning: If you try to insert duplicate _id → you get error:

JavaScript

3. Useful Options You Can Add (Beginner to Intermediate)

Both methods accept an options object as second argument.

JavaScript

4. Quick Cheat Sheet Table

What you want to do Command Example
Add one simple document db.students.insertOne({ name: “Sneha”, age: 19 })
Add many documents db.movies.insertMany([ {title:”RRR”}, {title:”Kalki”} ])
Use your own _id db.users.insertOne({ _id: “rahul123”, email: “rahul@hyd.in” })
Insert & get back the new _id Look at insertedId or insertedIds in the result
Insert date properly date: new Date() or ISODate(“2026-02-14”)
Bulk insert but don’t stop on error insertMany([…], { ordered: false })

5. Mini Exercise — Do This Right Now!

  1. use myLoveDb2026
  2. Insert one wish for yourself:
JavaScript
  1. Insert 2-3 more for friends/classmates.
  2. db.selfLove.find().pretty() → admire your data!

Understood everything? This is the foundation — every app starts with inserts!

Next class — what shall we do?

  • Find queries (read what we just inserted)?
  • Update and Delete to complete CRUD?
  • BulkWrite for mixed operations?
  • Or start a tiny “Valentine Wishlist” mini-project?

Tell me — I’m ready with the next lesson! 🚀❤️

Any doubt in insert? Ask freely — no judgment here! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *