Advanced Web Designing — Study Notes

Unit 01

NoSQL and MongoDB fundamentals — organized as exam-ready answers.

6 Questions 5 Marks Each Advanced Web Designing
01
5 Marks Question

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:

TypeDescriptionExamples
Document-OrientedStores data as flexible JSON/BSON-like documentsMongoDB, CouchDB
Key-ValueStores data as simple key-value pairsRedis, DynamoDB
Column-FamilyStores data in columns grouped into familiesCassandra, HBase
Graph DatabaseStores entities as nodes and relationships as edgesNeo4j, ArangoDB

Advantages (Pros) of NoSQL

Flexible Schema
No fixed schema; fields can vary between documents.
Horizontal Scalability
Can scale across multiple servers using techniques such as sharding.
High Performance
Optimized for fast reading and writing of large datasets.
Handles Big Data
Suitable for large amounts of structured and unstructured data.
High Availability
Replication helps keep data available even when a server fails.
Cost Effective
Can run on clusters of inexpensive commodity hardware.

Disadvantages (Cons) of NoSQL

Many NoSQL systems use the BASE/eventual consistency model rather than the strict ACID model traditionally associated with relational databases. This means data across replicas may not always be immediately consistent.
Also, unlike SQL databases, NoSQL does not have one standard query language; the query language varies between systems.
In shortNoSQL databases are especially suitable for big data, flexible schemas, distributed applications, and rapid scaling, with Document, Key-Value, Column-Family, and Graph being the four major types.
02
5 Marks Question

Explain the Features of NoSQL Databases

NoSQL databases provide several architectural features that make them suitable for large-scale, distributed, and flexible applications.

Schema-less Design
NoSQL databases do not require a fixed predefined schema. Documents in the same collection can have different fields, data types, and nested structures.
Distributed Architecture
Data can be distributed across multiple nodes or servers instead of being stored on a single server. This improves performance and allows the system to continue working even if individual nodes fail.
Eventual Consistency
Many NoSQL databases follow the BASE model. Instead of requiring every replica to contain the latest data immediately, replicas are allowed to become consistent over time.
CAP Theorem Trade-offs
NoSQL systems generally make trade-offs between Consistency (C), Availability (A), and Partition Tolerance (P). Many NoSQL systems prioritize Availability and Partition Tolerance over strict consistency.
JSON / BSON Support
Document-oriented NoSQL databases commonly store data using JSON or binary forms such as BSON in MongoDB. This structure maps naturally to objects used in modern programming languages.
Built-in Replication
NoSQL databases provide built-in data replication, where multiple copies of data are maintained across servers. This provides data redundancy and fault tolerance.
Auto-Sharding
Sharding divides a large dataset across multiple machines using a shard key. It allows the database to scale horizontally while applications can continue treating it as a single logical database.
ConclusionThe major features of NoSQL are schema-less design, distributed architecture, eventual consistency, CAP trade-offs, JSON/BSON support, replication, and auto-sharding, making it suitable for modern large-scale applications.
03
5 Marks Question

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.

01
CREATE — Insert Documents

MongoDB provides insertOne() to insert one document and insertMany() to insert multiple documents. If _id is not provided, MongoDB automatically generates a unique ObjectId.

insertOne() syntax
db.collectionName.insertOne(document)
Example
db.students.insertOne({
    name: "Riya Shah",
    age: 20,
    course: "BCA"
})
insertMany() example
db.students.insertMany([
    { name: "Aman", age: 21 },
    { name: "Priya", age: 22 }
])
02
READ — Find Documents

The find() method retrieves documents from a collection. A condition can be provided to filter the results.

Syntax
db.collectionName.find({ condition })
Example
db.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.

03
UPDATE — Modify Documents

updateOne() modifies the first matching document, while updateMany() modifies all matching documents.

updateOne() syntax & example
db.students.updateOne(
    { name: "Riya Shah" },
    { $set: { age: 21 } }
)
updateMany() example
db.students.updateMany(
    { course: "BCA" },
    { $set: { year: 2026 } }
)

Common update operators include $set, $unset, $inc, $rename, $push, and $pull.

04
DELETE — Remove Documents

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.

deleteOne() syntax & example
db.students.deleteOne(
    { name: "Karan Joshi" }
)
deleteMany() example
db.students.deleteMany(
    { course: "BCA" }
)
Quick SummaryCRUD → insertOne/Many() → find() → updateOne/Many() → deleteOne/Many(). These operations provide the basic mechanism for creating, retrieving, modifying, and removing documents in MongoDB.
04
5 Marks Question

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 syntax
db.collection.aggregate([
    { stage1 },
    { stage2 },
    ...
])
$match — Filter Documents
$match selects only documents that satisfy a given condition. It is similar to the WHERE clause in SQL.
$group — Group Documents
$group groups documents based on a field and performs calculations using operators such as $sum, $avg, $min, and $max. It is similar to GROUP BY in SQL.
$project — Select / Reshape Fields
$project controls which fields appear in the output. It can also rename fields and create computed fields.
$sort — Sort Documents
$sort arranges documents in ascending (1) or descending (-1) order.
$limit — Limit Results
$limit restricts the number of documents returned.
$unwind — Expand an Array
$unwind breaks an array into separate documents for each array element.
$lookup — Join Collections
$lookup performs a left outer join between two collections. It matches a field from the current collection with a field in another collection.
Complete pipeline example
db.students.aggregate([
    { $match: { course: "BCA" } },
    { $sort: { marks: -1 } },
    { $limit: 2 },
    { $project: { _id: 0, name: 1, marks: 1 } }
])
In shortThe Aggregation Framework processes data through multiple pipeline stages such as $match, $group, $project, $sort, $limit, $unwind, and $lookup, allowing MongoDB to perform complex data analysis and transformations efficiently.
05
5 Marks Question

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.

$set — Set or Add a Field
$set changes the value of an existing field. If the field does not exist, it creates the field.
$unset — Remove a Field
$unset completely removes a field from a document.
$inc — Increment / Decrement a Value
$inc increases or decreases a numeric field by a specified amount. A negative value can be used to decrease it.
$mul — Multiply a Value
$mul multiplies the value of a numeric field by a specified number.
$push — Add Element to an Array
$push adds a new element to an array field.
$pull — Remove Element from an Array
$pull removes a particular element from an array.
$addToSet — Add Only Unique Element
$addToSet adds an element to an array only if that element is not already present, thereby avoiding duplicates.
Upsert — Update or Insert
Upsert = Update + Insert. When { upsert: true } is specified, if a matching document exists it is updated, and if no matching document exists a new document is created.
Quick revision
OperatorFunction
$setSet/add a field
$unsetRemove a field
$incIncrease/decrease numeric value
$mulMultiply numeric value
$pushAdd element to array
$pullRemove element from array
$addToSetAdd unique element to array
upsertUpdate if found, insert if not found
In shortMongoDB update operators allow individual fields and arrays to be modified efficiently without replacing the entire document.
06
5 Marks Question

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.

BasisSQL DatabaseNoSQL Database
SchemaUses a fixed, predefined schema. All rows follow the defined table structure.Uses a flexible/schema-less structure. Documents can have different fields and structures.
ScalabilityMainly 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.
ConsistencyGenerally 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 CasesSuitable 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.
ExamplesMySQL, OracleMongoDB, Redis, Cassandra

Explanation of Major Differences

Schema
SQL requires the database structure to be defined beforehand. NoSQL allows records/documents to have different structures, making it easier to modify the data model as an application evolves.
Scalability
SQL typically scales vertically using a more powerful server, whereas NoSQL is designed to scale horizontally across multiple servers.
Consistency
SQL databases traditionally follow ACID for reliable transactions. NoSQL databases generally use the more relaxed BASE model, where replicas can become consistent over time.
Use Cases
NoSQL is especially useful for applications involving large volumes, high velocity, and varied forms of data, such as IoT data and modern web/mobile applications.
Real-world examples: SQL: MySQL and Oracle are examples of relational databases. NoSQL: MongoDB is commonly used in modern web/mobile applications and MEAN/MERN stack development; Redis is suitable for caching and session management, while Cassandra is suited to large write-heavy and analytical workloads.
ConclusionSQL is generally preferred for structured data and strict transactional consistency, while NoSQL is designed for flexibility, horizontal scalability, and large or rapidly changing datasets.