NoSQL (Not Only SQL) is a category of non-relational databases designed to store and manage structured, semi-structured and unstructured data with high scalability and flexibility. It covers:
- Flexible schema design for dynamic and evolving data.
- Multiple data models such as document, key-value, column-family and graph.
- High scalability through horizontal scaling across distributed systems.
- Fast read and write performance for handling large volumes of data.
1. What are the different types of NoSQL databases?
NoSQL databases are mainly classified into four types:
- Document Database: Stores data as JSON-like documents, allowing flexible and schema-less storage.
- Key-Value Database: Stores data as key-value pairs for very fast data access.
- Column-Family Database: Stores data in columns instead of rows, making it efficient for large datasets.
- Graph Database: Stores data as nodes and relationships for managing connected data.
2. Name some popular NoSQL databases.
- MongoDB: A popular document database used for flexible and scalable web applications.
- Redis: A high-speed in-memory database used for caching and session management.
- Apache Cassandra: A distributed database designed for high availability and large-scale data storage.
- CouchDB: A document database that supports JSON storage and data replication.
- Neo4j: A graph database used for applications with highly connected data.
- Amazon DynamoDB: A fully managed NoSQL database offering automatic scaling and high performance.
3. When should you use NoSQL?
- Use when your application requires a flexible (schema-less) database.
- Use it when you need to handle large volumes of data efficiently.
- Use it when high scalability is required through horizontal scaling.
- Use it when fast read and write performance is essential.
- Use it for storing semi-structured or unstructured data, such as JSON documents, logs and multimedia files.
4. What is the difference between relational (SQL) and non-relational (NoSQL) databases?
| SQL | NoSQL |
|---|---|
| Uses tables with a fixed schema. | Uses flexible, schema-less data models. |
| Best for structured data. | Best for structured, semi-structured and unstructured data. |
| Scales vertically. | Scales horizontally. |
For more Details refer this article: Difference between SQL and NOSQL
5. How do NoSQL databases store unstructured data?
NoSQL databases store unstructured data using flexible data models such as documents, key-value pairs, columns or graphs, allowing data to be stored without a fixed schema.
6. Does NoSQL use normalization?
NoSQL databases generally do not use normalization like relational databases. Instead, they prefer denormalization, where related data is stored together in a single document. This reduces the need for JOIN operations and improves query performance.
Example:
{
"name": "John",
"orders": [
{ "product": "Laptop" },
{ "product": "Mouse" }
]
}
In this example, the customer and order details are stored in the same document, allowing faster data retrieval without using JOINs.
7. What is the purpose of the CAP theorem in NoSQL databases?
The CAP theorem states that a distributed NoSQL database can guarantee only two of the following three properties at the same time:
- Consistency (C): Every read returns the latest data.
- Availability (A): Every request receives a response.
- Partition Tolerance (P): The database continues to operate despite network failures.
8. How can scalability be improved in a NoSQL database?
Scalability in a NoSQL database can be improved through horizontal scaling, where additional servers or nodes are added to the database cluster. Techniques such as data sharding, partitioning and replication help distribute data and workloads across multiple servers, improving performance, availability and fault tolerance.
9. How does indexing improve query performance in NoSQL?
Indexing improves query performance by allowing the database to quickly locate the required data instead of scanning the entire dataset, resulting in faster data retrieval and better overall performance.
10. What is a graph database?
A graph database is a type of NoSQL database that stores data as nodes (entities) and edges (relationships). It is designed to efficiently manage and query highly connected data.
Example: A social media platform uses a graph database to represent users (nodes) and their friendships (edges). Neo4j is a popular graph database.
11. How do NoSQL databases handle JOIN operations?
Most NoSQL databases do not support traditional SQL JOIN operations. Instead, they use embedded documents, references or denormalized data models to store related information together. This reduces the need for multiple queries and improves read performance.
Example: Instead of storing customer and order information in separate tables, MongoDB can store orders directly inside the customer document.
{
"_id": 1,
"name": "John",
"orders": [
{
"orderId": 101,
"amount": 1200
},
{
"orderId": 102,
"amount": 800
}
]
}
Retrieve the customer and all orders in a single query:
db.customers.find({ name: "John" })12. How do NoSQL databases query nested or embedded data?
NoSQL databases query nested or embedded data by accessing fields inside documents using dot notation (or an equivalent method, depending on the database).
Example (MongoDB):
Document:
{
"name": "Alice",
"address": {
"city": "America",
"zip": "110001"
}
}
Query:
db.users.find({ "address.city": "America" });This query returns all documents where the nested field address.city is "America".
13. What is the difference between embedding and referencing in MongoDB?
| Embedding | Referencing |
|---|---|
| Stores related data in the same document. | Stores related data in separate documents using references. |
| Provides faster read performance. | Reduces data duplication and improves data consistency. |
| Best for one-to-one or one-to-few relationships. | Best for one-to-many or many-to-many relationships. |
| Requires fewer queries to retrieve related data. | May require multiple queries or $lookup to retrieve related data. |
14. What are the common backup and recovery techniques in NoSQL databases?
Common backup and recovery techniques in NoSQL databases include full backups, incremental backups, snapshots and data replication. These methods help restore data quickly after failures or data loss.
Example (MongoDB):
Backup:
mongodump --db schoolRestore:
mongorestore dump/school15. How do you group documents and calculate aggregate values in MongoDB?
Use the $group stage to group documents and perform calculations such as sum, average, minimum and maximum.
Example:
db.orders.aggregate([
{
$group: {
_id: "$category",
totalSales: { $sum: "$amount" },
averageSales: { $avg: "$amount" }
}
}
]);
This query groups orders by category and calculates the total and average sales for each category.
16. What is the difference between a Document Database and a Key-Value Database?
A Document Database stores data as self-contained documents (such as JSON or BSON), while a Key-Value Database stores data as simple key-value pairs for fast data retrieval.
| Document Database | Key-Value Database |
|---|---|
| Stores data as JSON or BSON documents. | Stores data as key-value pairs. |
| Supports complex and nested data structures. | Stores simple values associated with unique keys. |
| Allows querying based on document fields. | Retrieves data using only the key. |
| Best for content management, e-commerce and user profiles. | Best for caching, session management and real-time applications. |
| Example: MongoDB, CouchDB | Example: Redis, Amazon DynamoDB (Key-Value mode) |
17. How do you configure a NoSQL database?
The configuration of a NoSQL database depends on the database system being used. It typically involves setting the database port, storage location, network settings and security options. Most NoSQL databases use configuration files to define these settings.
Example (MongoDB Configuration):
storage:
dbPath: /data/db
net:
port: 27017
security:
authorization: enabled
This configuration sets the database storage path, listens on port 27017 and enables user authentication.
18. What is BSON and how is it different from JSON?
BSON (Binary JSON) is the binary-encoded format used by MongoDB to store documents. It supports additional data types and provides faster data storage and retrieval than JSON.
| BSON | JSON |
|---|---|
| Binary format. | Text-based format. |
| Supports ObjectId, Date and Binary. | Supports basic data types only. |
| Used internally by MongoDB. | Used for data exchange between applications. |
Example (JSON):
{
"name": "harry",
"age": 25
}
Stored as BSON in MongoDB:
{
"_id": ObjectId("6868d1a8b5b87d8d6c12345"),
"name": "harry",
"age": 25,
"createdAt": ISODate("2026-07-03T10:30:00Z")
}
This example shows that BSON supports additional data types such as ObjectId and ISODate, which are not native JSON types.
19. What is the difference between find() and aggregate() in MongoDB?
| find() | aggregate() |
|---|---|
| Retrieves documents from a collection. | Processes documents through an aggregation pipeline. |
| Used for simple filtering and projection. | Used for complex operations like grouping, sorting, joining and calculations. |
| Faster for simple queries. | Better for data analysis and reporting. |
| Supports basic query conditions. | Supports stages such as $match, $group, $sort, $project and $lookup. |
20. How do NoSQL databases distribute data across multiple nodes?
NoSQL databases distribute data across multiple servers using sharding or partitioning. This improves scalability, balances workloads and prevents a single server from becoming a bottleneck.
Example (MongoDB):
sh.enableSharding("company");21. How do you design a scalable NoSQL database for an e-commerce application?
A scalable NoSQL database should use sharding to distribute data, replication for high availability and indexes to improve query performance. The data model should be designed to minimize expensive queries and support horizontal scaling.
Example (MongoDB):
sh.enableSharding("ecommerce");
sh.shardCollection(
"ecommerce.orders",
{ customerId: 1 }
);
22. How do you perform bulk write operations in MongoDB?
Bulk operations allow multiple insert, update and delete operations to be executed together, improving performance.
Example:
db.students.bulkWrite([
{
insertOne: {
document: {
name: "Alice",
age: 22
}
}
},
{
updateOne: {
filter: { name: "John" },
update: { $set: { age: 25 } }
}
}
]);
23. How do you find the top 5 customers with the highest total purchase amount?
Use the Aggregation Pipeline to group orders by customer, calculate the total purchase amount, sort the results in descending order and return the top five customers.
db.orders.aggregate([
{
$group: {
_id: "$customerId",
totalPurchase: { $sum: "$amount" }
}
},
{
$sort: {
totalPurchase: -1
}
},
{
$limit: 5
}
]);
24. Which NoSQL database would you choose for a caching system and why?
Redis is the preferred choice for caching because it stores data in memory, providing extremely fast read and write operations. It is commonly used for session management, caching and real-time applications.
Example:
SET user:101 "John"
GET user:101
The SET command stores data in the cache and the GET command retrieves it.