Define NoSQL Databases. List Different Types of NoSQL Databases. Also Explain Pros and Cons
Definition of NoSQL
NoSQL stands for “Not Only SQL.” It refers to database management systems that store and retrieve data using models other than the traditional row-and-column table structure of relational databases.
NoSQL databases generally provide a flexible/schema-less structure, support horizontal scaling, handle large amounts of unstructured data, and are designed for high-speed distributed systems.
Types of NoSQL Databases
There are four major types:
| Type | Description | Examples |
|---|---|---|
| Document-Oriented | Stores data as flexible JSON/BSON-like documents | MongoDB, CouchDB |
| Key-Value | Stores data as simple key-value pairs | Redis, DynamoDB |
| Column-Family | Stores data in columns grouped into families | Cassandra, HBase |
| Graph Database | Stores entities as nodes and relationships as edges | Neo4j, ArangoDB |
Advantages (Pros) of NoSQL
Disadvantages (Cons) of NoSQL
Explain the Features of NoSQL Databases
NoSQL databases provide several architectural features that make them suitable for large-scale, distributed, and flexible applications.
Explain CRUD Operations in MongoDB with Syntax and Examples
CRUD stands for Create, Read, Update, and Delete. These are the four basic operations used to manage data in MongoDB.
MongoDB provides insertOne() to insert one document and insertMany() to insert multiple documents. If _id is not provided, MongoDB automatically generates a unique ObjectId.
db.collectionName.insertOne(document)Exampledb.students.insertOne({
name: "Riya Shah",
age: 20,
course: "BCA"
})insertMany() exampledb.students.insertMany([
{ name: "Aman", age: 21 },
{ name: "Priya", age: 22 }
])The find() method retrieves documents from a collection. A condition can be provided to filter the results.
db.collectionName.find({ condition })Exampledb.students.find({ course: "BCA" })findOne() can be used when only one matching document is required.
db.students.findOne({ name: "Riya" })Query operators can also be used:
db.students.find({ age: { $gt: 18 } })Here, $gt means greater than.
updateOne() modifies the first matching document, while updateMany() modifies all matching documents.
db.students.updateOne(
{ name: "Riya Shah" },
{ $set: { age: 21 } }
)updateMany() exampledb.students.updateMany(
{ course: "BCA" },
{ $set: { year: 2026 } }
)Common update operators include $set, $unset, $inc, $rename, $push, and $pull.
deleteOne() removes the first matching document, while deleteMany() removes all documents matching the condition. Deleted documents cannot be automatically undone, so the filter should be used carefully.
db.students.deleteOne(
{ name: "Karan Joshi" }
)deleteMany() exampledb.students.deleteMany(
{ course: "BCA" }
)Explain MongoDB Aggregation Framework in Detail
The Aggregation Framework in MongoDB is used to process documents and return computed results. It works using an aggregation pipeline, where documents pass through a sequence of stages. Each stage performs an operation and passes its output to the next stage.
It is useful for operations such as filtering, grouping, calculating totals/averages, reshaping documents, sorting, and joining collections.
Basic syntaxdb.collection.aggregate([
{ stage1 },
{ stage2 },
...
])
db.students.aggregate([
{ $match: { course: "BCA" } },
{ $sort: { marks: -1 } },
{ $limit: 2 },
{ $project: { _id: 0, name: 1, marks: 1 } }
])
Explain Update Operators in MongoDB with Examples
Update operators in MongoDB are used with methods such as updateOne() and updateMany() to modify specific fields of existing documents without replacing the entire document.
{ upsert: true } is specified, if a matching document exists it is updated, and if no matching document exists a new document is created.| Operator | Function |
|---|---|
| $set | Set/add a field |
| $unset | Remove a field |
| $inc | Increase/decrease numeric value |
| $mul | Multiply numeric value |
| $push | Add element to array |
| $pull | Remove element from array |
| $addToSet | Add unique element to array |
| upsert | Update if found, insert if not found |
Compare SQL and NoSQL Databases
SQL databases are relational databases that store data in structured tables and rows, whereas NoSQL databases can store data using documents, key-value pairs, graphs, or column-family models.
| Basis | SQL Database | NoSQL Database |
|---|---|---|
| Schema | Uses a fixed, predefined schema. All rows follow the defined table structure. | Uses a flexible/schema-less structure. Documents can have different fields and structures. |
| Scalability | Mainly uses vertical scaling (scale-up) by increasing CPU, RAM, etc. of a server. | Designed for horizontal scaling (scale-out) by adding more servers using techniques such as sharding. |
| Consistency | Generally follows the ACID model, providing strong consistency and reliable transactions. | Generally follows the BASE model and may use eventual consistency to provide greater availability and scalability. |
| Use Cases | Suitable when data is highly structured and reliable transactions/consistency are important. | Suitable for big data, rapidly changing data, unstructured/semi-structured data, web/mobile applications and IoT. |
| Examples | MySQL, Oracle | MongoDB, Redis, Cassandra |