> For the complete documentation index, see [llms.txt](https://www.anandisheladiya.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.anandisheladiya.com/skills-and-experience/database/mongodb-nosql-database.md).

# MongoDB - NOSQL Database

MongoDB is a **document-oriented NoSQL database** used for high-volume data storage. Instead of using tables and rows as in relational databases, MongoDB uses **collections and documents**.

### **Key Features of MongoDB**

1. **Schema-less** – No fixed structure, allowing flexibility.
2. **Scalability** – Horizontally scalable using **sharding**.
3. **High Performance** – Fast read and write operations.
4. **Replication** – Provides **high availability** using replica sets.
5. **Indexing** – Supports different types of indexes to improve query performance.
6. **Aggregation Framework** – Enables complex data transformations.

### **MongoDB Architecture**

MongoDB follows a **distributed architecture** with multiple components:

1. **Client** – Applications interact with MongoDB using drivers.
2. **MongoDB Server** – Stores and manages the data.
3. **Replica Set** – Ensures **fault tolerance** and high availability.
4. **Sharded Cluster** – Enables horizontal scaling for large datasets.
5. **Config Servers** – Store metadata for sharded clusters.
6. **Mongos Router** – Distributes queries across shards.

<figure><img src="/files/T1smgawzpG0HBcMpdLhx" alt=""><figcaption></figcaption></figure>

### **Basic MongoDB Operations**

**1. Creating a Database**

```javascript
use myDatabase;

```

2. Creating a Collection

```javascript
db.createCollection("users");

```

3. Inserting a Document

```javascript
db.users.insertOne({
    name: "John Doe",
    email: "john@example.com",
    age: 28,
    created_at: new Date()
});

```

4. Finding Documents

```javascript
db.users.find({ age: { $gt: 25 } });

```

5. Updating a Document

```javascript
db.users.updateOne(
    { email: "john@example.com" },
    { $set: { age: 30 } }
);

```

6. Deleting a Document

```javascript
db.users.deleteOne({ email: "john@example.com" });

```

### **MongoDB Use Cases**

* **Big Data Applications**
* **Real-time Analytics**
* **Internet of Things (IoT)**
* **Content Management Systems**
* **Mobile and Web Apps**
