MongoDB Cheat Sheet (mongosh & Compass)

MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. This cheat sheet covers essential commands for using mongosh (MongoDB Shell) and MongoDB Compass.

MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. This cheat sheet covers essential commands for using mongosh (MongoDB Shell) and MongoDB Compass.


Connecting to MongoDB

Using mongosh (MongoDB Shell)

mongosh "mongodb://localhost:27017"

To connect with authentication:

mongosh "mongodb://username:password@localhost:27017/hck-db"

Using MongoDB Compass

  1. Open Compass.
  2. Click New Connection.
  3. Enter your connection string (e.g., mongodb://localhost:27017/hck-db).
  4. Click Connect.

πŸ“‚ Database Operations

Show all databases

show dbs

Create or Switch to the hck-db Database

MongoDB creates a database automatically when you insert the first document into a collection.

use hck-db

Delete the hck-db database

db.dropDatabase()

πŸ“‘ Collection Operations

Show all collections

show collections

Create Students and Classes collections

db.createCollection("Students")
db.createCollection("Classes")

Drop (delete) a collection

db.Students.drop()
db.Classes.drop()

CRUD Operations (Create, Read, Update, Delete)

Insert Documents

Insert a Student

db.Students.insertOne({ name: "Alice", age: 20, class: "Math 101" })

Insert Multiple Students

db.Students.insertMany([
  { name: "Bob", age: 22, class: "Physics 201" },
  { name: "Charlie", age: 21, class: "History 101" }
])

Insert a Class

db.Classes.insertOne({ name: "Math 101", instructor: "Dr. Smith", students: 30 })

Read Documents

Find One Student

db.Students.findOne({ name: "Alice" })

Find All Students

db.Students.find()

Find Students in a Specific Class

db.Students.find({ class: "Math 101" })

Find with Sorting

db.Students.find().sort({ age: -1 })

Update Documents

Update One Student

db.Students.updateOne(
  { name: "Alice" },
  { $set: { age: 21 } }
)

Update Multiple Students

db.Students.updateMany(
  { class: "Math 101" },
  { $set: { status: "active" } }
)

Update Class Information

db.Classes.updateOne(
  { name: "Math 101" },
  { $set: { instructor: "Dr. Johnson" } }
)

Delete Documents

Delete One Student

db.Students.deleteOne({ name: "Alice" })

Delete Multiple Students

db.Students.deleteMany({ age: { $gt: 21 } })

Delete a Class

db.Classes.deleteOne({ name: "Math 101" })

Indexing for Performance

Create an Index on Student Names

db.Students.createIndex({ name: 1 })

Show Indexes

db.Students.getIndexes()

Drop an Index

db.Students.dropIndex("name_1")

Aggregation

Count Students in Each Class

db.Students.aggregate([
  { $group: { _id: "$class", totalStudents: { $sum: 1 } } }
])

Find Average Age of Students

db.Students.aggregate([
  { $group: { _id: null, averageAge: { $avg: "$age" } } }
])

This cheat sheet provides the basics to get started with MongoDB using mongosh and Compass with hck-db, Students, and Classes. Happy coding! πŸš€