Mongo Shell (mongosh)

Interactive JavaScript interface for MongoDB

🐚 What is Mongo Shell?

Mongo Shell (mongosh) is an interactive JavaScript interface to MongoDB. It allows you to query, update data, and perform administrative operations directly from your terminal with simple commands.


// Connect to MongoDB and run a simple query
db.users.find({ age: { $gt: 18 } })
                                    

Getting Started with mongosh

🔌

Connect

Connect to MongoDB server

mongosh
📊

Query

Find and retrieve data

db.collection.find()
✏️

Insert

Add new documents

db.collection.insertOne({})
🔧

Update

Modify existing data

db.collection.updateOne()

🔹 Connecting to MongoDB

Start mongosh and connect to your MongoDB instance. By default, it connects to localhost on port 27017. You can also specify custom connection strings for remote databases or authentication.

# Connect to local MongoDB
mongosh

# Connect to remote MongoDB
mongosh "mongodb://username:password@host:port/database"

# Connect to MongoDB Atlas
mongosh "mongodb+srv://cluster.mongodb.net/myDatabase"

Output:

Current Mongosh Log ID: 507f1f77bcf86cd799439011
Connecting to: mongodb://127.0.0.1:27017
Using MongoDB: 6.0.0
test>

🔹 Basic Database Operations

Learn essential commands to work with databases and collections in MongoDB. These operations form the foundation of database management and data manipulation.

🔸 Show and Switch Databases

// Show all databases
show dbs

// Switch to a database (creates if doesn't exist)
use myDatabase

// Show current database
db

// Show collections in current database
show collections

Output:

admin   40.00 KiB
config  12.00 KiB
local   72.00 KiB
switched to db myDatabase
myDatabase

🔹 CRUD Operations

Create, Read, Update, and Delete operations are the core of database interactions. Master these commands to effectively manage your data in MongoDB collections.

🔸 Insert Documents

// Insert one document
db.users.insertOne({
  name: "John Doe",
  age: 25,
  email: "[email protected]"
})

// Insert multiple documents
db.users.insertMany([
  { name: "Alice", age: 30 },
  { name: "Bob", age: 22 }
])

🔸 Find Documents

// Find all documents
db.users.find()

// Find with condition
db.users.find({ age: { $gt: 25 } })

// Find one document
db.users.findOne({ name: "John Doe" })

// Pretty print results
db.users.find().pretty()

🔸 Update Documents

// Update one document
db.users.updateOne(
  { name: "John Doe" },
  { $set: { age: 26 } }
)

// Update multiple documents
db.users.updateMany(
  { age: { $lt: 25 } },
  { $set: { status: "young" } }
)

🔸 Delete Documents

// Delete one document
db.users.deleteOne({ name: "John Doe" })

// Delete multiple documents
db.users.deleteMany({ age: { $lt: 18 } })

// Delete all documents in collection
db.users.deleteMany({})

🔹 Useful Shell Helpers

MongoDB shell provides helpful commands for database administration and information retrieval:

Common Helper Commands:

  • help - Display help information
  • show dbs - List all databases
  • show collections - List collections in current database
  • show users - List users in current database
  • db.stats() - Display database statistics
  • db.collection.stats() - Display collection statistics
  • exit - Exit the shell
// Get database statistics
db.stats()

// Get collection statistics
db.users.stats()

// Count documents
db.users.countDocuments()

// Drop a collection
db.users.drop()

// Drop current database
db.dropDatabase()

🧠 Test Your Knowledge

Which command is used to switch to a database in mongosh?