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.
mongod
mongo
📂 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.
use myFirstDB
📦 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.
db.createCollection("users")
🧾 Step 5: Insert Your First Document
db.users.insertOne({
name: "Shivam",
age: 25,
email: "shivam@example.com"
})
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()
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
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
Such an informative content.
ReplyDeletegood job.Keep it up
ReplyDeleteNice blog
ReplyDeleteWell explained good blog
ReplyDeleteInformative Blog
ReplyDeleteWell explained 👍👍👍
ReplyDeleteExcellent , Great Work
ReplyDeleteVery informative
ReplyDeleteInformational excellent work
ReplyDeleteNice
ReplyDeleteVery Informative
ReplyDeleteExcellent work
ReplyDeleteWell explained
ReplyDeleteGreat work..!!
ReplyDeleteExcellent blog
ReplyDelete