MongoDB

πŸƒ MongoDB Cheat Sheet

[!info] 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

[!note] You can connect programmatically using drivers for:

Check out: MongoDB Drivers


  • databases

  • MariaDB Sheet

  • Metabase: Open Source Business Intelligence and Analytics

  • Php.md


🌍 Explore More


🏷️ Tags πŸ“š

#mongodb #nosql #database #mongo