๐Ÿƒ MongoDB Cheat Sheet

MongoDB is a flexible, scalable NoSQL document database used widely in modern application development. It stores data in JSON-like documents, making it ideal for unstructured or semi-structured data.


โš™๏ธ Installation

๐ŸชŸ Windows

  1. Download MongoDB from the official website.

  2. Run the installer and follow the setup wizard.


๐Ÿ macOS

Install via Homebrew:

brew tap mongodb/brew
brew install mongodb-community

๐Ÿง Linux

Ubuntu / Debian

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
 
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
 
sudo apt-get update
sudo apt-get install -y mongodb-org

Red Hat / CentOS

  1. Create a repo file:
sudo vi /etc/yum.repos.d/mongodb-org-5.0.repo
  1. Add:
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
  1. Then:
sudo yum install -y mongodb-org

๐Ÿ”„ Starting the MongoDB Service

Windows

mongod --install
net start MongoDB

macOS / Linux

sudo systemctl start mongod

๐Ÿงช Access MongoDB Shell

mongo

๐Ÿ’ป Basic MongoDB Commands

Tip

Use these commands within the mongo shell unless otherwise specified.

๐Ÿ“‹ Show Databases

show dbs

๐Ÿงญ Switch/Create Database

use your_database_name

๐Ÿ—ƒ๏ธ Create a Collection

db.createCollection("your_collection_name")

๐Ÿงพ Insert Document

db.your_collection_name.insertOne({ key1: "value1", key2: "value2" })

๐Ÿ” Find Documents

db.your_collection_name.find()

๐Ÿ”Ž Find with Filter

db.your_collection_name.find({ key: "value" })

๐Ÿ› ๏ธ Update One Document

db.your_collection_name.updateOne(
  { key: "value" },
  { $set: { new_key: "new_value" } }
)

๐Ÿ› ๏ธ Update Multiple Documents

db.your_collection_name.updateMany(
  { key: "value" },
  { $set: { new_key: "new_value" } }
)

โŒ Delete One Document

db.your_collection_name.deleteOne({ key: "value" })

โŒ Delete Multiple Documents

db.your_collection_name.deleteMany({ key: "value" })

๐Ÿง  Notes

You can connect programmatically using drivers for:



๐ŸŒ Explore More


๐Ÿท๏ธ Tags ๐Ÿ“š

mongodb nosql database mongo