MongoDB Basics
What is MongoDB?
MongoDB is a popular NoSQL database that provides high performance, high availability, and easy scalability. Unlike traditional relational databases, MongoDB stores data in flexible, JSON-like documents called BSON (Binary JSON), which allows for more dynamic and schema-less data storage.
Key Characteristics
Feature |
Description |
Document-Oriented |
Stores data in flexible, JSON-like documents |
Scalability |
Supports horizontal scaling through sharding |
Performance |
Provides high-speed read and write operations |
Indexing |
Supports multiple types of indexes for efficient querying |
MongoDB Architecture
graph TD
A[Client Application] --> B[MongoDB Server]
B --> C[Databases]
C --> D[Collections]
D --> E[Documents]
Installation on Ubuntu 22.04
To install MongoDB, use the following commands:
## Import MongoDB public GPG key
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
## Add MongoDB repository
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
## Update package lists
sudo apt-get update
## Install MongoDB
sudo apt-get install -y mongodb-org
Basic Concepts
- Database: A container for collections
- Collection: Equivalent to a table in relational databases
- Document: A record in a collection, similar to a row
- Field: A key-value pair in a document
Sample Document Structure
{
"_id": ObjectId("..."),
"name": "John Doe",
"age": 30,
"email": "[email protected]"
}
Why Choose MongoDB?
MongoDB is particularly useful for:
- Real-time analytics
- Content management systems
- IoT and time-series data
- Mobile and web applications
With LabEx, you can easily practice and learn MongoDB in a hands-on environment that supports practical skill development.