MongoDB Installation and Basic Operations
MongoDB Introduction
MongoDB is a popular NoSQL database designed for scalability, flexibility, and performance. It stores data in JSON-like documents, allowing for dynamic schemas and easy data modeling. MongoDB is widely used in web applications, IoT, and real-time data processing due to its ability to handle large volumes of unstructured data. Key features include support for horizontal scaling, replication for high availability, and a rich query language.
MongoDB Installation
Linux
To install MongoDB on Linux, follow these steps:
- Update the package list:
sudo apt update - Install MongoDB:
sudo apt install mongodb - Start the service and enable it to start on boot:
sudo systemctl start mongodb sudo systemctl enable mongodb - Verify the installation:
sudo systemctl status mongodb
macOS
For macOS, use Homebrew to install MongoDB:
- Install Homebrew (if not already installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" - Install MongoDB:
brew install mongodb - Start the service:
brew services start mongodb
Windows
On Windows, download the MongoDB Community Edition from the official website and follow these steps:
- Run the installer and select "Add to PATH" during installation.
- Start the service:
mongodb --service-start - Verify the installation:
mongod --version
MongoDB Community Edition
The MongoDB Community Edition is free and open-source, suitable for most local development and testing scenarios. It includes core features like data storage, querying, and replication.
Basic Operations
Core Commands
Create a Database and Collection:
mongo db_name db.createCollection("collection_name")Insert Documents:
db.collection_name.insertOne({ name: "John", age: 30 })Query Documents:
db.collection_name.find({ age: { $gt: 25 } })Update Documents:
db.collection_name.updateOne({ name: "John" }, { $set: { age: 31 } })Delete Documents:
db.collection_name.deleteOne({ name: "John" })
Database and Collection Management
- List Databases:
use db_name db.listDatabases() - List Collections:
use db_name db.listCollections()
Data Types
MongoDB supports various data types, including strings, numbers, dates, arrays, and nested documents. For example:
{
"name": "Alice",
"age": 25,
"birthDate": new Date(),
"tags": ["developer", "student"]
}
Indexing
Indices improve query performance by speeding up data retrieval. To create an index:
db.collection_name.createIndex({ name: 1 })
Indices can be created on fields, arrays, or combinations of fields.
Conclusion
MongoDB Installation and Basic Operations involve setting up the database, managing data through commands, and performing essential operations like querying, updating, and deleting documents. Understanding these fundamentals is critical for working with MongoDB in development and production environments. By following the outlined steps and leveraging core commands, users can efficiently manage data in a flexible, scalable NoSQL database.