UNIT -3
HALLTICKET NO: NAME:
Q1. Notes on Need of NoSQL?
Introduction to NoSQL
• NoSQL stands for "Not Only SQL".
• It is a class of non-relational database systems designed to handle large-scale
data storage and retrieval that is not well-suited to traditional relational
databases (RDBMS).
• NoSQL databases provide flexible schema, horizontal scalability, and high
performance for modern applications.
Limitations of Traditional SQL (Relational) Databases
Traditional RDBMS systems (like MySQL, PostgreSQL, Oracle) face several challenges in
modern application scenarios:
Limitation Description
Tables require predefined schemas; hard to adapt to
Rigid Schema
changing requirements.
Relational DBs scale up by adding hardware to one
Vertical Scaling
machine; expensive and limited.
Multiple joins across tables slow down performance in
Join Complexity
large data environments.
Inefficient for Not optimized for images, videos, documents, or JSON-
Unstructured Data like data.
Poor Performance at Struggles with large volumes of concurrent read/write
Scale operations.
Why NoSQL— Core Needs
1. Schema Flexibility
• NoSQL databases like MongoDB allow dynamic schemas, so fields can vary
across documents.
• Ideal for agile development, where data models evolve frequently.
2. Horizontal Scalability
• Supports scaling out (adding more servers) rather than just scaling up.
• This allows systems to handle massive traffic and data volumes, as seen in
modern web apps.
3. High Throughput and Low Latency
• Optimized for fast reads/writes.
• NoSQL systems are often used in real-time applications like social networks, e-
commerce, IoT, and gaming.
4. Big Data and Cloud Readiness
• Designed for distributed environments—a perfect match for cloud-based
applications.
• Enables partitioning and sharding to distribute data across clusters.
5. Better Support for Unstructured and Semi-Structured Data
• Can efficiently store and query data in JSON, XML, key-value pairs, graphs, or
binary objects.
• Useful for storing user profiles, logs, sensor data, media, etc.
6. High Availability and Fault Tolerance
• Supports replication and failover for building resilient applications.
• Critical for apps that must be available 24/7 globally.
Types of NoSQL Databases
Type Description Example Use Case Examples
Document- Stores data as JSON/BSON Content management, MongoDB,
based documents catalogs CouchDB
Key-Value Data stored as key-value Session management,
Redis, DynamoDB
Stores pairs caching
Column- Stores data in columns Data warehousing, Apache
based rather than rows analytics Cassandra, HBase
Type Description Example Use Case Examples
Stores entities as nodes Social networks, fraud Neo4j, Amazon
Graph-based
with relationships detection Neptune
When to Use NoSQL
Use NoSQL when:
• Your data is highly unstructured or semi-structured.
• The application requires real-time performance and low latency.
• You need scalability beyond a single server.
• You expect frequent schema changes.
• You’re building cloud-native, IoT, or mobile-first apps.
Q2. what you have been Understanding in MongoDB ?
MongoDB:
MongoDB is a NoSQL, open-source database based on the document model.
• It stores data in BSON (Binary JSON) format.
• Designed for high performance, high availability, and automatic scaling.
• Ideal for modern web applications and services that require a flexible, fast, and
scalable backend.
Key Features of MongoDB
Feature Description
Stores data in documents (like JSON objects) instead of
Document-Oriented
rows/columns.
Collections can hold documents with different
Schema-less
structures.
Scalable Supports horizontal scaling using sharding.
Feature Description
High Availability Through replication (Replica Sets).
Built-in Indexing Improves query performance.
Integrated Aggregation
Powerful for data analysis within the DB.
Framework
Core MongoDB Concepts
a. Collections
• Equivalent to tables in SQL.
• A collection is a group of related documents.
• Collections do not enforce schema, meaning documents can vary in structure.
b. Documents
• Analogous to rows in SQL, but much more flexible.
• Stored in BSON, a binary representation of JSON.
• Can include various data types (string, int, array, sub-document, etc.).
• Example:
"_id": ObjectId("..."),
"name": "Alice",
"age": 30,
"address": {
"city": "Mumbai",
"zip": "400001"
},
"hobbies": ["reading", "cycling"]
c. _id Field
• Unique identifier for each document.
• Generated automatically unless specified.
• Composed of:
o Timestamp (4 bytes)
o Machine ID (3 bytes)
o Process ID (2 bytes)
o Counter (3 bytes)
MongoDB Data Types
• Stored in BSON format.
• Common types:
o String
o Integer
o Boolean
o Double
o Date
o Object
o Array
o Null
o ObjectId
BSON allows embedding documents and arrays, unlike flat relational rows.
Document Modeling Approaches
a. Normalization (Using References)
• Stores related data in separate collections.
• Ideal for many-to-many or frequently updated data.
• Reduces duplication.
• Cons: Slower queries due to manual joins via application code.
b. Denormalization (Using Embedded Documents)
• Embeds related data within the main document.
• Ideal for one-to-one or one-to-few relationships.
• Fast retrieval.
• Cons: Increased redundancy; more storage needed.
Capped Collections
• Fixed-size collections.
• Automatically overwrite oldest documents when full.
• Use case: Logging systems, circular buffers.
• Benefits:
o Maintains insertion order.
o No deletion logic required.
• Limitations:
o Cannot grow document size after insertion.
o Cannot delete individual documents manually.
Atomic Operations:
• MongoDB supports atomicity at the document level.
• Means a single document’s changes are fully completed or not at all.
• For multi-document operations, use transactions (available in newer versions).
Indexing, Sharding, and Replication
a. Indexing
• Improves query performance.
• _id is automatically indexed.
• Other fields can be indexed manually.
b. Sharding
• Splits data across multiple servers.
• Enables horizontal scaling.
• Shards handle large datasets and high throughput.
c. Replication
• Uses Replica Sets (primary-secondary).
• Provides high availability and failover support.
Performance Considerations
Concern Solution
Document Growth Avoid large updates that increase size; consider normalization.
Large Collections Break into multiple collections or shard the data.
Query Speed Use indexing and denormalization appropriately.
Data Lifecycle Management
• Implement TTL (Time-To-Live) indexes to automatically delete old documents.
• Use capped collections for auto-cleanup.
• Plan for archiving and cleanup policies.
Usability vs. Performance
• MongoDB offers flexibility and performance, but trade-offs exist.
• Evaluate based on:
o Read/write patterns
o Data access frequency
o Application scalability
o Future schema changes
Q3: MongoDB Data Types
MongoDB supports a variety of data types that are used to store different kinds of
data in its documents (which are stored in BSON format — Binary JSON). Here's a
list of the commonly used MongoDB data types:
• Common types:
o String
o Integer
o Boolean
o Double
o Date
o Object
o Array
o Null
o ObjectId
1. String (String)
• Most commonly used data type.
• Used to store text.
• Example: "name": "John"
2. Integer (int32, int64)
• Used to store numeric values without decimals.
• int32 for 32-bit integers, int64 for 64-bit integers.
• Example: "age": 25
3. Double (double)
• Used for floating point numbers.
• Example: "price": 49.99
4. Boolean (bool)
• Stores true or false.
• Example: "isActive": true
5. Array (array)
• Stores multiple values in an ordered list.
• Can hold different data types.
• Example: "skills": ["Python", "MongoDB", "Node.js"]
6. Object (object)
• Used to store embedded documents.
• Example:
"address": {
"city": "New York",
"zip": "10001"
7. Null (null)
• Represents a null or missing value.
• Example: "middleName": null
8. ObjectId (objectId)
• A special 12-byte ID automatically generated by MongoDB as the _id field.
• Example: "_id": ObjectId("60df1f77bcf86cd799439011")
9. Date (date)
• Stores date and time in UTC.
• Example: "createdAt": ISODate("2025-05-31T10:00:00Z")
10. Timestamp (timestamp)
• Used mainly for internal MongoDB operations.
• Not commonly used for application-level data.
11. Binary Data (binData)
• Used to store binary data like images or files.
12. Decimal128 (decimal)
• High-precision decimal type useful for financial or scientific applications.
• Example: "amount": NumberDecimal("12345.6789")
13. Regular Expression (regex)
• Used to store regular expressions.
• Example: "pattern": /abc/i
14. JavaScript (javascript)
• Stores JavaScript code (rarely used).
• Example: "func": function() { return true; }
15. Symbol (symbol)
• Similar to string, but used internally for languages that support symbols.
16. Min/Max Key
• MinKey and MaxKey are used to compare values against the lowest and
highest BSON elements.
• Mostly used in internal operations and queries.
Q4 . Planning Your Data Model
When planning your MongoDB schema, answer the following:
1. What are the core objects the application will use?
2. What are the relationships between those objects?
o One-to-One
o One-to-Many
o Many-to-Many
3. How often will objects be added?
4. How often will objects be deleted?
5. How frequently will objects be updated?
6. How often will they be queried?
7. How will they be accessed?
o By ID
o By property values
o Through comparisons
8. How will groups of objects be accessed?
o Common ID
o Common properties
Data Modeling Techniques
1. Normalization (Using References)
• Breaks down large documents into multiple collections
• Ideal for one-to-many or many-to-many relationships
• Reduces duplication and storage usage
• Example: User profile with a favorite_store field pointing to a stores collection
Pros:
• Avoids data redundancy
• Easier to update shared subobjects
Cons:
• Requires joins or multiple queries
• Can impact performance due to extra lookups
2. Denormalization (Using Embedded Documents)
• Embeds related data directly into the main document
• Ideal for one-to-one or small one-to-many relationships
• Improves read performance
Pros:
• Single document retrieval (faster)
• No joins required
Cons:
• Data duplication
• Larger document sizes
• Harder to update shared fields
Design Tip:
Denormalize when reads are frequent and subdata rarely changes. Normalize when
subdata is shared across many documents or changes often.
Atomic Operations
• MongoDB provides atomic writes at the document level.
• Denormalized documents allow atomic updates.
• Normalized structures need multiple writes → not atomic across documents.
If atomicity is critical → embed data.
Document Growth Considerations
• MongoDB allocates extra space for document updates.
• If a document outgrows its space:
o It must be moved on disk → performance hit
o Leads to disk fragmentation
Normalize properties likely to grow (like arrays of items).
Indexing, Sharding & Replication
Indexing
• Speeds up query performance
• _id is automatically indexed
• Add indexes on frequently queried fields
Sharding
• Splits large collections across multiple servers
• Supports horizontal scaling
Replication
• Copies data across servers for high availability
• Ensures data reliability
Large Collections vs Many Collections
• No major performance hit from having many collections
• Avoid very large collections with unrelated data
• Example: Store user transaction history in separate collections per user instead
of one massive history collection
Data Life Cycle Planning
• Define how long documents should live
• Use TTL (Time To Live) to auto-delete old documents
• Techniques:
o TTL indexes
o Capped collections
o Custom cleanup scripts
Final Thoughts on Usability & Performance
• Your schema must prioritize:
o Usability: Can the app retrieve data easily and correctly?
o Performance: Can the database handle the load efficiently?
Often, you'll need to iterate between usability and performance to get the balance
right
Q5: Administering User Accounts
Administering User Accounts in MongoDB
1. MongoDB Authentication
• MongoDB uses role-based access control (RBAC) to govern access.
• Authentication verifies the identity of users.
• Authorization determines their access levels.
2. Creating an Admin User
Before enabling access control, create a user in the admin database:
use admin
db.createUser({
user: "admin",
pwd: "securepassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
})
3. Starting MongoDB with Authentication
Enable access control by starting MongoDB with --auth:
mongod --auth --dbpath /data/db
4. Creating a Regular User
Switch to the appropriate database and create a user:
use yourDatabase
db.createUser({
user: "appUser",
pwd: "appPassword",
roles: [ { role: "readWrite", db: "yourDatabase" } ]
})
5. User Management Commands
• List users:
db.getUsers()
• Update user password:
db.changeUserPassword("username", "newPassword")
• Drop a user:
db.dropUser("username")
Q6: Configuring Access Control
Configuring Access Control in MongoDB
1. Authentication Mechanisms
• Default: SCRAM-SHA-256 (secure challenge-response).
• Other supported mechanisms: LDAP, x.509, Kerberos.
2. Enabling Authorization
To enforce access control:
1. Add to mongod.conf:
2. security:
3. authorization: enabled
4. Restart MongoDB.
3. Built-in Roles
Role Description
read Read-only access to data.
readWrite Read and write access to data.
dbAdmin Database-level administrative tasks.
userAdmin User management on a DB.
readWriteAnyDatabase Global read/write access.
userAdminAnyDatabase Create/manage users in all DBs.
root Superuser role.
5. Custom Roles
Creating Custom Roles
You can define your own roles using:
db.createRole({
role: "customReadWrite",
privileges: [
resource: { db: "testDB", collection: "testCollection" },
actions: [ "find", "insert", "update", "remove" ]
],
roles: []
})
Granting Roles to Users
db.grantRolesToUser("adminUser", [{ role: "readWrite", db: "testDB" }])
Revoking Roles from Users
db.revokeRolesFromUser("adminUser", [{ role: "readWrite", db: "testDB" }])
Verifying Access Control
Checking a User’s Roles
db.getUser("adminUser")
Using the usersInfo Command
db.runCommand({ usersInfo: "adminUser" })
Example Workflow
Start MongoDB with access control:
mongod --auth
Connect to the server without authentication and create the first user (the admin
user):
mongo
use admin
db.createUser({
user: "siteAdmin",
pwd: "adminPass",
roles: [ { role: "root", db: "admin" } ]
})
Authenticate with the new user:
db.auth("siteAdmin", "adminPass")
Create a new application user:
use appDB
db.createUser({
user: "appUser",
pwd: "userPass",
roles: [ { role: "readWrite", db: "appDB" } ]
})
Q7: Write a complete notes on Managing Collections in MongoDB ?
• A collection in MongoDB is equivalent to a table in relational databases.
• It is a group of documents that share a similar structure.
• Collections do not enforce schemas, so documents within can have different
fields and data types.
Viewing Collections
show collections
• Use after switching to the target database with use <dbName>.
Creating a Collection
Method 1: Implicit Creation
• Just insert a document into a non-existing collection:
db.products.insertOne({ name: "Item A", price: 100 });
Method 2: Explicit Creation with Options
db.createCollection("products", {
capped: false,
size: 10485760, // in bytes (optional for capped)
max: 1000 // optional: max number of documents
});
Deleting a Collection
Drop a Collection
db.products.drop()
• Returns true if collection was dropped successfully.
Finding Documents in a Collection
db.products.find()
• Returns all documents in the products collection.
Filtered Find
db.products.find({ price: { $gt: 50 } })
Inserting Documents into a Collection
Single Document
db.products.insertOne({ name: "Item B", price: 150 });
Multiple Documents
db.products.insertMany([
{ name: "Item C", price: 200 },
{ name: "Item D", price: 250 }
]);
Deleting Documents from a Collection
Single Document
db.products.deleteOne({ name: "Item C" });
Multiple Documents
db.products.deleteMany({ price: { $lt: 100 } });
Updating Documents in a Collection
Update Specific Fields
db.products.updateOne(
{ name: "Item B" },
{ $set: { price: 180 } }
);
Update Multiple Documents
db.products.updateMany(
{ price: { $gt: 200 } },
{ $inc: { price: 10 } }
);
Collection Stats
Get Stats for a Collection
db.products.stats()
• Returns document count, storage size, index info, etc.
Q8: Adding the MongoDB Driver to Node.js, Connecting to MongoDB
from Node.js
Step 1: Install MongoDB and Dependencies
Make sure you have MongoDB installed and running.
Then, install the necessary Node.js module:
npm install mongodb
Step 2: Create the Node.js Application
Save the following code as mongodb_operations.js.
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const dbName = 'foodDeliveryDB';
async function run()
const client = new MongoClient(url);
try {
await client.connect();
console.log("Connected to MongoDB!");
const db = client.db(dbName);
const collection = db.collection('orders');
// **1. Insert a document**
const newOrder = { customer: "S.RAJENDER", food: "Pizza", price: 200 };
await collection.insertOne(newOrder);
console.log("Order inserted successfully!");
// **2. Read documents**
const orders = await collection.find().toArray();
console.log("All Orders:", orders);
// **3. Update a document**
await collection.updateOne({ customer: "S.RAJENDER" }, { $set: { food: "Burger",
price: 150 } });
console.log("Order updated successfully!");
// **4. Delete a document**
// await collection.deleteOne({ customer: "S.RAJENDER" });
// console.log("Order deleted successfully!");
} catch (err) {
console.error("Error:", err);
} finally {
// Close the database connection
await client.close();
console.log("Connection closed.");
// Run the function
run();
Q9: Understanding the Objects Used in the MongoDB Node.js Driver,
Understanding the Objects Used in the MongoDB Node.js Driver
The MongoDB Node.js Driver allows Node.js applications to connect to
and interact with MongoDB databases. The driver exposes several
important objects that you work with when performing operations such
as connecting to the database, inserting data, querying, and managing
collections.
Key Objects in the MongoDB Node.js Driver
1. MongoClient
• Purpose: The main entry point to interact with MongoDB.
• Role: Used to establish a connection to the database.
• Import Example:
const { MongoClient } = require('mongodb');
Usage:
const client = new MongoClient(uri);
await client.connect();
const db = client.db('myDatabase');
2. Db
• Purpose: Represents a MongoDB database instance.
• Obtained From: client.db('databaseName')
• Usage:
const db = client.db('myDatabase');
const collection = db.collection('users');
3. Collection
• Purpose: Represents a collection (table equivalent) in the
database.
• Usage:
Insert data:
await collection.insertOne({ name: 'Alice' });
Find data:
const user = await collection.findOne({ name: 'Alice' });
4. Cursor
• Purpose: Represents the result of a query that returns multiple
documents.
Returned By: collection.find()
Usage:
const cursor = collection.find({ age: { $gt: 18 } });
await cursor.forEach(doc => console.log(doc));
5. ObjectId
• Purpose: A special object used to represent document IDs in
MongoDB.
Required For: Querying by _id field.
Import:
const { ObjectId } = require('mongodb');
Usage:
const doc = await collection.findOne({ _id: new ObjectId('...') });
6. WriteResult, UpdateResult, DeleteResult
• Purpose: These objects contain the outcome of write, update,
and delete operations.
• Examples:
Insert:
const result = await collection.insertOne({ name: 'Bob' });
console.log(result.insertedId);
Update:
const result = await collection.updateOne({ name: 'Bob' }, {
$set: { age: 30 } });
console.log(result.modifiedCount);
Delete:
const result = await collection.deleteOne({ name: 'Bob' });
console.log(result.deletedCount);
7. BulkWriteOperation (Advanced)
• Purpose: Used for batch operations (insert, update, delete) in a
single call.
• Usage:
const result = await collection.bulkWrite([
{ insertOne: { document: { name: 'X' } } },
{ updateOne: { filter: { name: 'X' }, update: { $set: { age: 25 } } } },
{ deleteOne: { filter: { name: 'X' } } }
]);
Summary Table
Object Description Common Use
Connects to
MongoClient Connection setup
MongoDB server
Represents a
Db MongoDB Selecting a database
database
Represents a Inserting, querying,
Collection
collection updating docs
Holds results of a Iterating over query
Cursor
query results
BSON ID used in Querying documents
ObjectId
MongoDB by _id
WriteResult / UpdateResult Operation Inspecting the result
/ DeleteResult outcome objects of DB operations
Performance-
Batch multiple
BulkWriteOperation optimized bulk
operations
operations
Q10: Accessing and Manipulating Databases, Accessing and
Manipulating Collections
MongoDB is a NoSQL database that stores data in flexible, JSON-like
documents. Node.js is a popular JavaScript runtime often used to build
web applications. Together, they provide a powerful backend development
combination.
To interact with MongoDB in Node.js, we use the MongoDB Node.js driver or
an ODM (Object Document Mapper) like Mongoose.
2. Setup
Installing MongoDB Node.js Driver
npm install mongodb
Importing MongoDB
const { MongoClient } = require('mongodb');
3. Connecting to MongoDB
const url = 'mongodb://localhost:27017'; // MongoDB connection URL
const client = new MongoClient(url);
async function connectToMongo() {
try {
await client.connect();
console.log("Connected to MongoDB");
} catch (err) {
console.error(err);
}
}
connectToMongo();
4. Accessing a Database
const db = client.db('myDatabase'); // Access or create a database named
'myDatabase'
5. Accessing a Collection
const collection = db.collection('myCollection'); // Access or create a
collection named 'myCollection'
6. CRUD Operations
A. Create (Insert)
Insert One Document
await collection.insertOne({ name: 'Alice', age: 25 });
Insert Many Documents
await collection.insertMany([
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 28 }
]);
B. Read (Find)
Find One Document
const user = await collection.findOne({ name: 'Alice' });
console.log(user);
Find All (with filter or all documents)
const users = await collection.find({}).toArray();
console.log(users);
C. Update
Update One Document
await collection.updateOne(
{ name: 'Alice' },
{ $set: { age: 26 } }
);
Update Many Documents
await collection.updateMany(
{ age: { $gt: 25 } },
{ $set: { status: 'active' } }
);
D. Delete
Delete One Document
await collection.deleteOne({ name: 'Bob' });
Delete Many Documents
await collection.deleteMany({ age: { $lt: 30 } });
7. Listing All Databases and Collections
List Databases
const databasesList = await client.db().admin().listDatabases();
databasesList.databases.forEach(db => console.log(db.name));
List Collections in a Database
const collections = await db.listCollections().toArray();
collections.forEach(col => console.log(col.name));
8. Closing the Connection
await client.close();
9. Example: Complete Script
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
async function main() {
try {
await client.connect();
const db = client.db('schoolDB');
const students = db.collection('students');
await students.insertOne({ name: "John", age: 20 });
const student = await students.findOne({ name: "John" });
console.log(student);
await students.updateOne({ name: "John" }, { $set: { age: 21 } });
await students.deleteOne({ name: "John" });
} finally {
await client.close();
}
}
main().catch(console.error);