MongoDB Get Started

Your first steps with MongoDB

🚀 Getting Started with MongoDB

Start your MongoDB journey in minutes! This guide walks you through the quickest ways to begin using MongoDB, from cloud-based solutions to local installations.


// Your first MongoDB command
show dbs
                                    

Output:

admin   0.000GB
config  0.000GB
local   0.000GB

Three Ways to Get Started

Choose the method that best fits your learning style and needs. MongoDB Atlas offers the fastest setup with no installation required, while local installation gives you complete control over your development environment.

☁️

MongoDB Atlas

Cloud database (Recommended)

Free Tier No Setup Instant
💻

Local Installation

Install on your computer

Full Control Offline Free
🐳

Docker Container

Run MongoDB in Docker

Portable Isolated Quick

🔹 Option 1: MongoDB Atlas (Cloud)

The fastest way to start - no installation needed! MongoDB Atlas provides a free cloud database that's perfect for learning and development.

Steps to Get Started:

  1. Visit mongodb.com/cloud/atlas
  2. Click "Try Free" and create an account
  3. Create a free cluster (takes 3-5 minutes)
  4. Create a database user and password
  5. Get your connection string
// Connection string example
mongodb+srv://username:password@cluster0.mongodb.net/myDatabase

// Connect using MongoDB Shell
mongosh "mongodb+srv://cluster0.mongodb.net/myDatabase" --username myUser

🔹 Option 2: Local Installation

Install MongoDB Community Edition on your computer for offline development:

🔸 Windows Installation

# Download MongoDB installer from mongodb.com
# Run the .msi installer
# Choose "Complete" installation
# Install as a Windows Service

# Start MongoDB
net start MongoDB

# Connect to MongoDB
mongosh

🔸 macOS Installation

# Using Homebrew
brew tap mongodb/brew
brew install mongodb-community

# Start MongoDB
brew services start mongodb-community

# Connect to MongoDB
mongosh

🔸 Linux Installation

# Ubuntu/Debian
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
sudo apt-get install -y mongodb-org

# Start MongoDB
sudo systemctl start mongod

# Connect to MongoDB
mongosh

🔹 Option 3: Docker Container

Run MongoDB in a Docker container for easy setup and cleanup:

# Pull MongoDB image
docker pull mongo

# Run MongoDB container
docker run -d -p 27017:27017 --name mongodb mongo

# Connect to MongoDB
docker exec -it mongodb mongosh

# Stop container
docker stop mongodb

# Remove container
docker rm mongodb

Output:

Current Mongosh Log ID: 6a7b8c9d0e1f2a3b4c5d6e7f
Connecting to: mongodb://127.0.0.1:27017
Using MongoDB: 7.0.0
test>

🔹 Verify Your Installation

Test that MongoDB is working correctly:

// Check MongoDB version
db.version()

// Create a test database
use testDB

// Insert a test document
db.testCollection.insertOne({ message: "Hello MongoDB!" })

// Find the document
db.testCollection.find()

// Show all databases
show dbs

Output:

7.0.0
switched to db testDB
{ acknowledged: true, insertedId: ObjectId("...") }
{ _id: ObjectId("..."), message: "Hello MongoDB!" }
admin    0.000GB
config   0.000GB
local    0.000GB
testDB   0.000GB

🔹 Install MongoDB Shell (mongosh)

The MongoDB Shell is a command-line interface for interacting with MongoDB:

Download mongosh:

  • Visit mongodb.com/try/download/shell
  • Select your operating system
  • Download and install
  • Verify: mongosh --version
# Connect to local MongoDB
mongosh

# Connect to MongoDB Atlas
mongosh "mongodb+srv://cluster0.mongodb.net" --username myUser

# Connect to specific database
mongosh "mongodb://localhost:27017/myDatabase"

🔹 Your First Database Operations

Try these basic commands to get familiar with MongoDB:

// Create/switch to database
use myFirstDB

// Insert data
db.users.insertOne({
    name: "Alice",
    email: "alice@example.com",
    age: 28
})

// Query data
db.users.find()

// Count documents
db.users.countDocuments()

// Drop database
db.dropDatabase()

Output:

switched to db myFirstDB
{ acknowledged: true, insertedId: ObjectId("65a1b2c3d4e5f6789...") }
{ _id: ObjectId("..."), name: "Alice", email: "alice@example.com", age: 28 }
1
{ ok: 1, dropped: "myFirstDB" }

🧠 Test Your Knowledge

What is the recommended way for beginners to start with MongoDB?