Table of Contents
What is MongoDB?
MongoDB is a NoSQL (Not Only SQL) database management system. Initially developed by the 10gen company in 2009, MongoDB is a document-oriented database that works with documents in BSON (Binary JSON) format, similar to JSON. Unlike relational databases, MongoDB has a flexible schema, meaning each document can have different fields.
Key features of MongoDB include:
- Flexible Schema: Unlike relational databases that typically have a predefined schema, MongoDB allows for dynamic and flexible document structures.
- Document-Oriented: Data is stored in documents in BSON format, resembling JSON. Documents are grouped into collections.
- High Performance: MongoDB achieves high performance by storing data in BSON format, indexing documents, and providing efficient query capabilities. It is also designed for scalability, making it effective for large datasets.
- Indexing: MongoDB supports field-based indexing for quick data access, improving query performance.
- Horizontal Scalability: MongoDB offers horizontal scalability for handling large datasets and high-traffic applications. Adding new servers is facilitated to enhance system performance.
MongoDB is commonly chosen for applications requiring large and complex data structures, especially in web applications and large-scale projects. Its JSON-like document format and flexible schema empower developers with greater freedom in data modeling.
Installing MongoDB on Ubuntu:
- Add the Official MongoDB Repositories:
sudo apt update sudo apt install gnupg wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
- Add the MongoDB Repositories:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
- Update Packages and Install MongoDB:
sudo apt update sudo apt install mongodb-org
- Start the MongoDB Service:
sudo systemctl start mongod
- Ensure MongoDB Starts on Boot:
sudo systemctl enable mongod
- Check the Status of MongoDB:
sudo systemctl status mongod
By following these steps, you can install MongoDB on Ubuntu. Please note that for other operating systems (Windows, macOS), different installation steps may be required. You can find OS-specific installation instructions on the MongoDB Installation page on the official MongoDB website.
Keep in mind that after installing MongoDB, you’ll need a MongoDB client for connection and database management. This typically involves using a MongoDB driver in a programming language or using a graphical user interface program.
MongoDB Cheat Sheet
Basic Operations:
- Connect to MongoDB:
mongo
- Show Available Databases:
show dbs
- Switch to a Database:
use <database_name>
- Show Collections in the Current Database:
show collections
Data Operations:
- Insert a Document:
db.collection_name.insert({ key: "value" })
- Query Documents:
db.collection_name.find({ key: "value" })
- Update Documents:
db.collection_name.update({ key: "old_value" }, { $set: { key: "new_value" } })
- Remove Documents:
db.collection_name.remove({ key: "value" })
Indexing:
- Create an Index:
db.collection_name.createIndex({ key: 1 })
- List Indexes on a Collection:
db.collection_name.getIndexes()
- Drop an Index:
db.collection_name.dropIndex("index_name")
Aggregation:
- Aggregate Data:
db.collection_name.aggregate([{ $group: { _id: "$key", count: { $sum: 1 } } }])
- Sort Documents:
db.collection_name.find().sort({ key: 1 })
Administration:
- Show Server Status:
db.runCommand({ serverStatus: 1 })
- Show Build Info:
db.runCommand({ buildInfo: 1 })
- Backup Database:
mongodump --db <database_name> --out <backup_directory>
- Restore Database:
mongorestore --db <database_name> --dir <backup_directory>
This cheat sheet covers some common MongoDB operations. Be sure to check the official MongoDB documentation for more in-depth information and details on advanced features.