Alluding to “not only SQL,” or NoSQL databases, are non-relational databases that employ a distinct data storing mechanism from relational tables. They are perfect for applications that need to be developed quickly and scaled quickly because of their flexible schemas that allow them to handle massive amounts of data and heavy user loads. Based on their data model, NoSQL databases can be classified as document, key-value, wide-column, or graph databases.
Example of a NoSQL Database: MongoDB
MongoDB is a popular document-based NoSQL database that stores data in BSON (Binary JSON) format. This format allows for the storage of data in a flexible, JSON-like format, making it easy to work with in applications.
Key Features of MongoDB:
- Document-Oriented: MongoDB stores data in a document format, similar to JSON, which can include nested arrays and objects. This allows for flexible and rich data structures.
- Schema-Less: Unlike relational databases, MongoDB does not require a fixed schema, allowing for dynamic and flexible data models.
- Scalability: MongoDB is designed to scale out by distributing data across multiple servers. It supports horizontal scaling through sharding, which helps in handling large datasets and high loads.
- High Availability: MongoDB provides high availability through replica sets, which are groups of MongoDB servers that maintain the same data set.
- Indexing: MongoDB supports secondary indexes, allowing for efficient data retrieval. You can create indexes on any field in the document, enhancing query performance.
Example of Using MongoDB:
Suppose you are developing a social media application that stores user profiles and their posts. In MongoDB, you might store a user’s profile and posts in a single document, like so:
{
"_id": "dharmu9898",
"name": "Dharmendra kumar",
"email": "dharmu9898@gmail.com",
"posts": [
{
"title": "My First Post",
"content": "Hello, Avi!",
"date": "2024-02-29T10:00:00Z"
},
{
"title": "Updates on MongoDB",
"content": "MongoDB is great for social media apps.",
"date": "2024-03-01T12:00:00Z"
}
]
}
With this structure, user profiles and posts can be efficiently queried, and adding new posts to a person’s profile is simple and doesn’t require complicated joins.