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:

  1. Update the package list:
    sudo apt update  
    
  2. Install MongoDB:
    sudo apt install mongodb  
    
  3. Start the service and enable it to start on boot:
    sudo systemctl start mongodb  
    sudo systemctl enable mongodb  
    
  4. Verify the installation:
    sudo systemctl status mongodb  
    

macOS

For macOS, use Homebrew to install MongoDB:

  1. Install Homebrew (if not already installed):
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  
    
  2. Install MongoDB:
    brew install mongodb  
    
  3. Start the service:
    brew services start mongodb  
    

Windows

On Windows, download the MongoDB Community Edition from the official website and follow these steps:

  1. Run the installer and select "Add to PATH" during installation.
  2. Start the service:
    mongodb --service-start  
    
  3. 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

  1. Create a Database and Collection:

    mongo db_name  
    db.createCollection("collection_name")  
    
  2. Insert Documents:

    db.collection_name.insertOne({ name: "John", age: 30 })  
    
  3. Query Documents:

    db.collection_name.find({ age: { $gt: 25 } })  
    
  4. Update Documents:

    db.collection_name.updateOne({ name: "John" }, { $set: { age: 31 } })  
    
  5. 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.