MongoDB Beginner Guide: How to Create Your First Database and Collection

MongoDB Beginner Guide: How to Create Your First Database and Collection

MongoDB is a powerful NoSQL database that stores data in flexible, JSON-like documents. If you're just beginning with MongoDB, this blog will help you create your first database and collection step by step.

🧰 What You'll Learn

- How to create a MongoDB database

- How to create a collection

- How to insert your first document

- How to view your data

📥 Step 1: Install MongoDB (Skip if Already Installed)

Visit the MongoDB official download page (https://www.mongodb.com/try/download/community) and install the Community Server version. After installation, make sure you can run 'mongod' and 'mongo' commands from your terminal or command prompt.

🟢 Step 2: Start the MongoDB Server

Run the following command to start the MongoDB server:

mongod

Then open another terminal window and run:

mongo

This opens the MongoDB shell, where you'll run all your commands.

📂 Step 3: Create Your First Database

Use the following command in the shell to create (or switch to) a database:

use myFirstDB

Note: The database will be created only when you insert your first piece of data.

📦 Step 4: Create a Collection

Collections are like tables in SQL. You can create one like this:

db.createCollection("users")

Or simply insert data directly and MongoDB will auto-create it.

🧾 Step 5: Insert Your First Document


db.users.insertOne({
  name: "Shivam",
  age: 25,
  email: "shivam@example.com"
})

🔍 Step 6: View Your Data

Use the following command to view your inserted data:

db.users.find().pretty()

✅ Summary

You've now successfully:

- Created a MongoDB database

- Created a collection

- Inserted your first document

- Viewed the data

💡 Next Steps

- Learn how to update and delete documents

- Explore advanced queries

- Try MongoDB Atlas for cloud databases

📌 Quick Code Recap


use myFirstDB
db.createCollection("users")
db.users.insertOne({
  name: "Shivam",
  age: 25,
  email: "shivam@example.com"
})
db.users.find().pretty()

👦🏻 Name:Shivam Khude

🏫 College: Sri Balaji University, Pune

🏫 School: School of Computer Studies

🎓 Class: TY-BCA(E)




Comments

Post a Comment

Popular posts from this blog

Query Operator's

Creating Documents in MongoDB(Insert)