0% found this document useful (0 votes)
2 views29 pages

NoSQL Database Concepts Notes

NoSQL_Database_Concepts

Uploaded by

Shreya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views29 pages

NoSQL Database Concepts Notes

NoSQL_Database_Concepts

Uploaded by

Shreya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NoSQL Database Concepts

Complete Study Notes — All Units

Covering: Introduction · MongoDB · CRUD · Indexing · PHP Integration

Subject NoSQL Database Concepts

Total Units 5 Units (Unit I – Unit V)

Syllabus Introduction · MongoDB · Working with MongoDB · Indexing · PHP Integration

Note All content compiled from lecture slides. Examples included for every concept.
Table of Contents
Unit I — Introduction to NoSQL Databases
› What is NoSQL?
› History of NoSQL
› Features of NoSQL
› Types of NoSQL Databases
› Limitations of RDBMS
› Advantages of NoSQL
› RDBMS vs NoSQL / SQL vs NoSQL
› CAP Theorem
› When to Use NoSQL

Unit II — Introduction to MongoDB


› What is MongoDB?
› Features of MongoDB
› RDBMS vs MongoDB (Terminology)
› MongoDB Compass GUI
› Installation & Configuration
› Creating Database & Collection
› Basic Operations: insert, find, limit, skip, projection

Unit III — Working with MongoDB


› Database & Collection Creation
› Drop Operations
› Insert Documents
› Querying Documents
› Conditional Operators
› Updating Documents
› Deleting Documents
› Relationships: Embedded & Referenced
› Data Backup & Restoration
› Cursors & JSON Printing

Unit IV — Sorting and Indexing Techniques


› Sorting with sort()
› Indexing Overview
› Creating / Dropping Indexes
› Types of Indexes
› Index Properties: Unique, Partial, Sparse
› Covered Queries
› Aggregation & MapReduce
› Replication – Concepts & Setup
› Sharding – Concepts & Architecture
› Replication vs Sharding

Unit V — PHP with MongoDB Integration


› Setting Up PHP + MongoDB Environment
› CRUD Operations in PHP: Create, List, Update, Retrieve, Filter, Delete
› CAP Theorem (Revisited)
› Sorting in PHP context
› Sharding & Replication Differences
UNIT 1 | Introduction to NoSQL Databases

1.1 What is NoSQL?


NoSQL stands for "Not Only SQL" (sometimes interpreted as "No SQL"). It refers to a broad class of
database management systems that do not follow the traditional relational model used by SQL-based
databases. NoSQL databases are designed to handle large volumes of structured, semi-structured,
unstructured, and polymorphic data with high performance and scalability.

Unlike RDBMS which stores data in fixed rows and columns (tables), a NoSQL database is flexible — data
can be stored as key-value pairs, documents (JSON/XML), wide-column stores, or graphs. This makes it
extremely suitable for modern web applications, real-time analytics, and Big Data use cases.

Real-World Examples of NoSQL Adoption

• Twitter — stores billions of tweets using NoSQL for high write throughput

• Facebook — uses NoSQL for user activity feeds, messages, and social graphs

• Google — uses Bigtable (a column-family store) for indexed web data

• Amazon — uses DynamoDB (key-value store) for millions of product catalogue entries

1.2 History of NoSQL


In the mid-1990s, the internet gained massive popularity. Relational databases, designed for structured
business data, struggled to keep up with the explosion of unstructured web content — photos, messages,
log files, and user-generated content. This mismatch motivated the development of non-relational
databases.

The term NoSQL was coined to represent databases that avoided the rigid table structure of SQL.
Organizations like Google, Amazon, Facebook, Twitter, and LinkedIn adopted NoSQL systems to handle
their scale. NoSQL systems can handle both structured and unstructured Big Data quickly, which
traditional RDBMS could not do efficiently.

1.3 Features of NoSQL Databases


NoSQL databases are characterized by the following key features:

■ Non-Relational
NoSQL databases never follow the relational model. They do not use flat fixed-column tables. Instead they
work with self-contained aggregates or BLOBs (Binary Large Objects). There is no need for
object-relational mapping or complex data normalization.

■ Schema-Free / Dynamic Schema


NoSQL databases are either schema-free or have relaxed schemas. You can insert data WITHOUT
defining the schema first. This enables real-time application changes without service interruptions, making
development faster and more flexible. Different documents in the same collection can have entirely
different fields.

■ Auto-Sharding
NoSQL performs horizontal scaling — instead of upgrading a single server (vertical scaling), it distributes
data across multiple servers. This auto-sharding feature automatically spreads data across machines. If a
server fails, it is replaced quickly without disrupting the application.

■ Replication
NoSQL databases support automatic replication across multiple servers. This ensures high availability and
disaster recovery. Data can be distributed across multiple geographic regions to withstand regional
failures. NoSQL is self-healing — no separate application is needed for replication.

■ Integrated Caching
NoSQL databases have built-in caching. Frequently accessed data is kept in system memory, removing
the need for a separate caching layer (like Memcached or Redis in some setups).

■ Simple API
NoSQL offers simple interfaces for storing and querying data. Text-based protocols like HTTP REST with
JSON are commonly used. Most NoSQL systems do not use a standard query language — they have their
own API or query mechanisms.

1.4 Limitations of Relational Databases (Why NoSQL?)


Traditional RDBMS (Relational Database Management Systems) have several limitations that led to the
rise of NoSQL:

• Fixed Schema: Every table must have a predefined structure. Adding new fields requires altering the
table.
• Scalability Issues: RDBMS scales vertically (adding RAM/CPU to one server), which is expensive.
• Performance Degradation: With massive volumes of data (terabytes), RDBMS query performance
becomes slow.
• Not Suitable for Unstructured Data: RDBMS cannot efficiently store images, videos, JSON
documents, logs.
• Complex Joins: Queries across multiple tables require complex JOINs which are slow and hard to
maintain.
• High Cost of Horizontal Scaling: Distributing RDBMS across multiple machines is complex and
expensive.

1.5 Advantages of NoSQL


• Highly scalable — horizontal scaling by adding more machines (scale-out)
• Handles large volumes of unstructured/semi-structured data
• High performance — faster read/write operations
• Flexible schema — no need to define data structure upfront
• Supports distributed data storage across multiple locations
• Cost-effective — uses commodity hardware instead of high-end servers
• Suitable for real-time applications and Big Data analytics
• Built-in support for replication and fault tolerance

1.6 Types of NoSQL Databases


NoSQL databases are broadly categorized into four types based on how they store and retrieve data:

1.6.1 Key-Value Stores


Data is stored as a simple key-value pair, similar to a dictionary or hash map. The key is unique and the
value can be anything — a string, JSON object, BLOB, integer, etc. This is the simplest and fastest type of
NoSQL database.

Best for: Shopping carts, session management, caching, user profiles.

Examples: Redis, Amazon DynamoDB, Riak, Memcached.

Example Entry: Key = "user:1001" → Value = {name: "Arjun", city: "Chennai"}

1.6.2 Column-Oriented Databases


Inspired by Google's Bigtable, column-oriented NoSQL databases store data in columns instead of rows.
Each column is stored separately and contiguously on disk. This allows very fast aggregation queries
(SUM, COUNT, AVG, MIN, MAX) since only the relevant column needs to be read, not the entire row.

Best for: Data warehouses, business intelligence, CRM, library catalogues.

Examples: Apache Cassandra, HBase, Hypertable.

1.6.3 Document-Oriented Databases


Data is stored as documents — typically in JSON or XML format. Unlike key-value stores where the value
is opaque, a document database understands the structure of the value and allows querying on fields
within the document. Each document is self-describing.

Documents within the same collection can have different fields (dynamic schema). This is the most popular
type of NoSQL database for web applications.

Best for: CMS, blogging, e-commerce, real-time analytics.

Examples: MongoDB, CouchDB, Amazon SimpleDB, Lotus Notes.

1.6.4 Graph Databases


Graph databases store data as nodes (entities) and edges (relationships). Every node and edge has a
unique identifier and can hold properties. This makes it ideal for use cases where relationships between
entities are as important as the entities themselves — like social networks, recommendation engines, or
fraud detection.

Best for: Social networks, logistics, spatial data, knowledge graphs.

Examples: Neo4J, OrientDB, Infinite Graph, FlockDB.

1.7 RDBMS vs NoSQL — Detailed Comparison


Feature RDBMS (SQL) NoSQL

Data Model Tables with rows and columns Key-value, Document, Column, Graph

Schema Fixed predefined schema Dynamic / Schema-free

Scalability Vertical (scale up — add RAM) Horizontal (scale out — add servers)

Query Language SQL (Structured Query Language) Own APIs; some support SQL-like syntax

Joins Supports complex multi-table JOINs No joins; uses embedded/referenced docs

ACID Compliance Full ACID compliance BASE model (Basically Available, Soft state, Eventually consist

Performance Slower with very large data Faster for large unstructured data

Best Use Case Transactions, financial data Big Data, real-time web, social apps

Examples MySQL, PostgreSQL, Oracle MongoDB, Cassandra, Redis, Neo4J

1.8 MySQL vs MongoDB


MySQL is a popular open-source relational database, while MongoDB is the leading document-oriented
NoSQL database. Below is a quick comparison:

Aspect MySQL MongoDB

Data Format Rows and columns in tables JSON-like BSON documents

Schema Rigid, predefined Flexible, dynamic

Relationships Foreign keys and JOINs Embedded documents / $lookup

Scalability Primarily vertical Horizontal (sharding built-in)

Transactions Full ACID support Multi-document ACID (v4.0+)

Query SQL MQL (MongoDB Query Language) + JSON

Speed Slower for large unstructured data ~100x faster for document-based access

Use Case Banking, ERP, accounting Social media, analytics, catalogs

1.9 CAP Theorem


The CAP Theorem (also known as Brewer's Theorem, proposed by Eric Brewer in 2000) states that a
distributed database system can guarantee at most two of the following three properties simultaneously:

C — Consistency A — Availability P — Partition Tolerance

Every read receives the most Every request receives a response The system continues operating even if
recent write or an error. All nodes (not an error). The system remains some messages between nodes are
see the same data at the same operational. lost or delayed.
time.

In reality, network partitions always occur in distributed systems. Therefore, P (Partition Tolerance) is a
must, and systems must choose between C and A:

• CP Systems (Consistency + Partition Tolerance): HBase, MongoDB, Zookeeper — return errors if


availability cannot be guaranteed.
• AP Systems (Availability + Partition Tolerance): Cassandra, CouchDB, DynamoDB — always respond
but data may be slightly outdated.
• CA Systems (Consistency + Availability): Traditional RDBMS like MySQL/PostgreSQL — work only
when there is no network partition.

★ RDBMS follows ACID; NoSQL follows BASE (Basically Available, Soft state, Eventually
consistent).

1.10 When to Use NoSQL


Choose NoSQL over SQL in the following scenarios:

• Dealing with large volumes of unstructured or semi-structured data (logs, social media, sensor data)
• Need for horizontal scalability across many commodity servers
• Rapid development with evolving data models (no fixed schema required)
• Real-time applications like gaming, instant messaging, live dashboards
• Social networking platforms requiring graph-like relationships
• Content Management Systems (CMS) and e-commerce catalogues with varying product attributes
• When the dataset is very large and price of vertical scaling is too high

Note: NoSQL requires programming knowledge. Unlike SQL which can be learned by non-technical staff,
NoSQL usually requires a coding background.
UNIT 2 | Introduction to MongoDB

2.1 What is MongoDB?


MongoDB is a cross-platform, open-source, document-oriented NoSQL database. It provides high
performance, high availability, and easy horizontal scalability. MongoDB stores data in flexible, JSON-like
documents called BSON (Binary JSON), meaning fields can vary from document to document and data
structure can be changed over time.

MongoDB was developed by 10gen (now MongoDB Inc.) and first released in 2009. It is written in C++
and works on the concept of Collections and Documents.

2.2 Features of MongoDB


■ Document-Oriented
Stores data as BSON (Binary JSON) documents. Each document is a complete, self-contained unit of
data.

■ Schema-less
Collections do not enforce a fixed schema. Documents in the same collection can have different fields.

■ High Performance
MongoDB is approximately 100 times faster than traditional RDBMS for document-based operations. Uses
internal memory for working sets.

■ Horizontal Scalability
Built-in sharding support distributes data across multiple servers automatically.

■ Rich Query Language


Supports dynamic queries on documents using a query language nearly as powerful as SQL.

■ Indexing
Supports indexes on any field in the document, including embedded fields, for faster query execution.

■ Aggregation
Provides a powerful aggregation framework similar to SQL's GROUP BY, COUNT, SUM, etc.

■ Replication
Supports Replica Sets for high availability and automatic failover.

■ GridFS
Built-in support for storing large files (like images, videos) that exceed BSON document size limit.
■ Ad-hoc Queries
Supports search by field, range queries, and regular expressions in real-time.

2.3 RDBMS vs MongoDB — Terminology Mapping


If you are familiar with relational databases, the following table maps SQL terminology to MongoDB
equivalents:

RDBMS Term MongoDB Term Description

Database Database A container for collections (same concept)

Table Collection Group of related documents

Row / Tuple Document A single record (stored as BSON/JSON)

Column Field A key in a document

Primary Key _id field Auto-generated unique identifier (ObjectId)

Table Join Embedded Document / $lookup Relationships via nesting or aggregation pipeline

Index Index Same concept, supports single, compound, and text indexes

SQL Query MongoDB Query (MQL) Different syntax but similar logical operations

2.4 Advantages of MongoDB over RDBMS


• MongoDB is schema-less — a collection can hold documents with different fields. In RDBMS, you
must define the schema before inserting any data.
• There is no need for object-relational mapping (ORM) — objects map naturally to documents.
• MongoDB is horizontally scalable via sharding. RDBMS is only vertically scalable (adding RAM),
which is expensive.
• MongoDB emphasizes the CAP theorem; RDBMS emphasizes ACID properties.
• MongoDB is best for hierarchical data and nested structures, while RDBMS struggles with deeply
nested data.
• MongoDB supports both JSON query language and SQL-like syntax; RDBMS supports only SQL.
• MongoDB is easy to set up and configure. No complex installation required.
• MongoDB is ~100x faster than traditional RDBMS for document-based access patterns.
• No support for complex joins means simpler query design for document-centric applications.
• Supports deep queryability — dynamic queries on documents including nested fields.

2.5 MongoDB Compass (GUI)


MongoDB Compass is the official Graphical User Interface (GUI) for MongoDB. It allows users to visually
explore and interact with their MongoDB data without writing query syntax. Using Compass, you can
create databases, collections, insert/update/delete documents, create indexes, and analyze query
performance — all through a visual interface.

Key Features of MongoDB Compass:


• Schema visualization — understand the structure of your data visually
• Query builder — build queries without writing code
• Index management — create, view, and drop indexes
• Aggregation Pipeline builder — construct complex aggregation queries visually
• Performance insights — view query execution statistics
• Document editor — edit documents in JSON, Table, or List view

Installing MongoDB Compass:


• Step 1: Visit: [Link]
• Step 2: Select the installer (available as .exe, .msi, or .zip for Windows)
• Step 3: Click Download and run the installer
• Step 4: Follow the installation wizard
• Step 5: Once installed, Compass launches automatically — configure privacy settings
• Step 6: Connect using a connection string from MongoDB Atlas OR enter host/port manually

2.6 MongoDB Installation & Configuration


Installation Steps (Windows):
• Step 1: Download MongoDB MSI Installer from the official MongoDB website.
• Step 2: Run the installer and select Complete setup type.
• Step 3: Accept the license agreement.
• Step 4: Select "Run service as Network Service user" — note the data directory path.
• Step 5: Optionally deselect MongoDB Compass if installing separately.
• Step 6: Click Install and then Finish.

Configuration Steps:
After installation, create the necessary folders and configure MongoDB as a Windows service:
C:\Program Files\MongoDB\data\db (stores all database files)
C:\Program Files\MongoDB\log\ (stores log files)

Open Command Prompt (as Administrator) and run:


mongod --directoryperdb --dbpath "C:\Program Files\MongoDB\data\db" --logpath "C:\Program
Files\MongoDB\log\[Link]" --logappend --rest --install

Then start MongoDB as a Windows service:


net start MongoDB

You should see the message: "MongoDB service was started successfully"
UNIT 3 | Working with MongoDB

3.1 Core Data Concepts


Database
A database in MongoDB is a physical container for collections. Each database gets its own set of files on
the filesystem. A single MongoDB server typically has multiple databases. A database is created
automatically when you first store data in it.

Collection
A collection is a group of MongoDB documents — equivalent to a table in RDBMS. Collections exist
within a database and do not enforce a schema. Documents within a collection can have different fields.
All documents in a collection typically serve a similar purpose.

Document
A document is a set of key-value pairs (similar to a JSON object). Documents have a dynamic schema,
meaning documents in the same collection do not need the same set of fields or structure. Common fields
may even hold different types of data across documents.

Example document:
{ "_id": ObjectId("507f1f77bcf86cd799439011"), "name": "Ravi Kumar", "age": 21, "course":
"[Link] Computer Science", "marks": [85, 90, 78] }

3.2 Creating Databases and Collections


3.2.1 Creating a Database
Use the use command to select or create a database. If the database does not exist, MongoDB creates it
automatically when you first insert data.
use myDatabase

Output: switched to db myDatabase

3.2.2 Creating a Collection


The easiest way to create a collection is to insert a document into it:
[Link]({ name: "Alice", age: 20, course: "MCA" })

MongoDB automatically creates the students collection if it does not exist.

3.2.3 Creating a Collection via Compass


• Step 1: Click on the Create Collection button in the Collection window.
• Step 2: Fill in the collection name in the Create Collection dialog box.
• Step 3: Click Create Collection to confirm.
3.3 Drop Operations
3.3.1 Drop a Collection
To permanently delete a collection and all its documents:
[Link]()

3.3.2 Drop a Database


To delete an entire database (first switch to it):
use myDatabase [Link]()

3.3.3 Drop via Compass


• Step 1: In the Collection window, click the trash icon next to the collection.
• Step 2: A confirmation dialog appears — enter the collection name.
• Step 3: Click Drop Collection to confirm deletion.

3.4 Inserting Documents


3.4.1 insert() — Insert Single or Multiple Documents
// Single document [Link]({ name: "Bob", age: 22 }) // Multiple documents
(array) [Link]([ { name: "Carol", age: 21 }, { name: "Dave", age: 23 } ])

3.4.2 Inserting Arrays of Embedded Documents


var myEmployee = [ { EmployeeId: "abc", EmployeeName: "Mohan" }, { EmployeeId: "def",
EmployeeName: "Smith" } ] [Link](myEmployee)

3.4.3 Printing Output in JSON Format


To view documents in a nicely formatted JSON output:
[Link]().forEach(printjson)

3.5 Querying Documents


3.5.1 find() — Retrieve All Documents
[Link]() // Returns all documents [Link]().pretty() // Formatted
output

3.5.2 Conditional Operators for Querying


MongoDB supports a rich set of conditional operators for filtering documents:

Operator Meaning Syntax Example

(none) Equal to { age: 21 }


Operator Meaning Syntax Example

$lt Less than { age: { $lt: 25 } }

$gt Greater than { salary: { $gt: 40000 } }

$lte Less than or equal { age: { $lte: 24 } }

$gte Greater than or equal { age: { $gte: 18 } }

$ne Not equal to { city: { $ne: "Delhi" } }

$in In a list of values { name: { $in: ["Alice","Bob"] } }

$and Logical AND { $and: [{ age: { $gt: 18 } }, { city: "Chennai" }] }

$or Logical OR { $or: [{ age: 20 }, { age: 21 }] }

3.5.3 Query Examples


// Equal: City = Delhi [Link]({ city: "Delhi" }) // Less than: age < 23
[Link]({ age: { $lt: 23 } }) // Greater than: salary > 40000
[Link]({ salary: { $gt: 40000 } }) // Not equal: city != Delhi
[Link]({ city: { $ne: "Delhi" } })

3.6 Projection
MongoDB Projection allows you to retrieve only specific fields from documents, rather than all fields. This
improves performance and reduces network data transfer.
Syntax: db.collection_name.find({}, { field1: 1, field2: 1 }) // 1 = include field, 0 =
exclude field // _id is always included unless explicitly excluded // Example: Get only
name and age [Link]({}, { name: 1, age: 1, _id: 0 }) // Example: Exclude salary
field [Link]({}, { salary: 0 })

Important Rules: Set field value to 1 to include, 0 to exclude. You cannot mix inclusion and exclusion in
the same query (except for _id).

3.7 Limit() and Skip() Methods


3.7.1 limit() — Control Number of Results
The limit() method restricts the number of documents returned by a query. This is useful for pagination.
Syntax: db.collection_name.find().limit(number) // Return only first 5 students
[Link]().limit(5) // Find students with id > 2002, limit to 3
[Link]({ id: { $gt: 2002 } }).limit(3)

3.7.2 skip() — Skip Initial Results


The skip() method skips the specified number of documents in the result set. Combined with limit(), it
enables efficient pagination.
Syntax: db.collection_name.find().skip(number) // Skip first 5, return next 5 (page 2)
[Link]().skip(5).limit(5)

3.8 Updating Documents


MongoDB provides the update() method to modify existing documents. The $set operator is used to
update specific fields without affecting other fields.
Syntax: [Link](SELECTION_CRITERIA, UPDATED_DATA) // Update a single field
[Link]( { course: "java" }, { $set: { course: "android" } } ) // Update
multiple documents [Link]( { city: "Chennai" }, { $set: { status:
"active" } } )

★ Always use $set operator when updating. Without $set, MongoDB REPLACES the entire
document.

3.9 Deleting Documents


MongoDB provides three methods for deleting documents:

3.9.1 deleteOne() — Delete Single Document


Deletes only one document even if multiple documents match the filter criteria.
[Link]({ name: { $in: ["Dennis Ritchie", "Bjarne Stroustrup"] } }) //
Only ONE document is deleted even though two match

3.9.2 deleteMany() — Delete Multiple Documents


Deletes all documents that match the filter criteria.
[Link]({ name: { $in: ["Dennis Ritchie", "Bjarne Stroustrup"] } }) //
Both matching documents are deleted

3.9.3 remove() — Delete by Criteria or All


// Delete all documents matching criteria [Link]({ name: "James Gosling"
}) // Delete ALL documents in collection [Link]({})

3.10 Cursor in MongoDB


When you call [Link](), MongoDB returns a cursor — a pointer to the result set of the query.
The cursor allows you to iterate over the matching documents. You can apply methods like limit(), skip(),
and sort() to the cursor.
var myCursor = [Link]() while ([Link]()) { printjson([Link]())
}

3.11 Relationships in MongoDB


MongoDB supports two approaches to model relationships between data: Embedded Documents and
Referenced Documents.

3.11.1 Embedded Documents (Denormalized)


Instead of separate tables, related data is embedded directly inside a parent document as a
sub-document. This is like including the full address inside the user document.
{ name: "Ravi", address: { street: "12 MG Road", city: "Chennai", pincode: "600001" } }

Advantages of Embedded Documents

• Faster reads (all data in one document)

• Atomic updates on the document

• Good when data is always accessed together

• Fixed file size for predictable storage

3.11.2 Referenced Documents (Normalized)


The child document is stored separately and referenced by its _id in the parent document. Similar to
foreign keys in RDBMS, but without enforced referential integrity.
// Parent document { _id: ObjectId("abc"), name: "Order #1", customer_id: ObjectId("xyz")
} // Child document (in customers collection) { _id: ObjectId("xyz"), name: "Ravi Kumar",
email: "ravi@[Link]" }

Advantages of Referenced Documents

• No data duplication

• File size is flexible

• Better for large related datasets

• Use $lookup in aggregation to join collections

Use $lookup in the aggregation pipeline to perform a JOIN-like operation:


[Link]([ { $lookup: { from: "customers", // Foreign collection localField:
"customer_id", // Field in orders foreignField: "_id", // Field in customers as:
"customerInfo" // Output array field } } ])

3.12 Data Backup and Restoration


Data backup is a critical operation for any database system. MongoDB provides two command-line utilities
for this purpose:

mongodump — Create Backup


mongodump exports the data from a running MongoDB instance into BSON files stored in a dump/
directory. It reads the data and creates a space-efficient backup.
mongodump # Backup all databases mongodump --db myDatabase # Backup specific database
mongodump --db myDB --collection users # Backup specific collection
mongorestore — Restore Backup
mongorestore reads BSON data files from a dump/ directory and restores them into a running MongoDB
instance.
mongorestore # Restore all databases mongorestore --db myDatabase dump/myDatabase
UNIT 4 | Sorting and Indexing Techniques

4.1 Sorting with sort()


The sort() method in MongoDB is used to sort the result of a query. It must be applied to the cursor before
retrieving documents. A value of 1 sorts in ascending order; -1 sorts in descending order.
Syntax: db.collection_name.find().sort({ field: 1 }) // Ascending
db.collection_name.find().sort({ field: -1 }) // Descending // Multi-field sort
[Link]().sort({ field1: 1, field2: -1 }) // Example: Sort students by name
A-Z [Link]().sort({ name: 1 }) // Example: Sort students by name Z-A
[Link]().sort({ name: -1 })

★ A stable sort returns the same result every time on same data. An unstable sort may return
different results.

4.2 Indexing in MongoDB


An index is a special data structure that stores a small portion of the collection's data in an ordered format.
Without indexes, MongoDB performs a Collection Scan — scanning every document to find matches.
With indexes, MongoDB can quickly locate the right documents, dramatically improving query
performance.

Indexing improves performance for:

• find() queries — locate documents faster


• Range queries using $lt, $gt, $lte, $gte
• Sort operations — sort() uses indexes to avoid in-memory sorting
• Aggregation operations involving filtering, grouping, and sorting

4.2.1 Creating an Index — createIndex()


Syntax: [Link]({ field: 1 }) // Ascending index
[Link]({ field: -1 }) // Descending index // Compound index on two
fields [Link]({ name: 1, city: 1 }) // Old method (still works):
[Link]({ name: 1 })

4.2.2 Viewing Indexes — getIndexes()


[Link]() // Returns details of all indexes on the collection

4.2.3 Dropping Indexes


// Drop a single index [Link]({ name: 1 }) // Drop multiple indexes
[Link]({ name: 1, city: 1 }) // Drop ALL indexes (except _id)
[Link]()

4.3 Types of Indexes in MongoDB


4.3.1 Single Field Index
An index on a single field of a document. MongoDB can traverse this index in both ascending and
descending order, so the direction (1 or -1) doesn't matter much for single-field indexes.
[Link]({ name: 1 }) // Output after sorting by name: { name: "amit",
city: "ujjain" } { name: "ankitt", city: "indore" } { name: "rohit", city: "ujjain" } {
name: "sumit", city: "devas" }

4.3.2 Compound Index


An index on multiple fields. The order of fields in a compound index matters — MongoDB sorts by the
first field, then within that by the second field, and so on.
[Link]({ name: 1, city: 1 }) // Sorts by name first, then by city within
same names

4.3.3 Multikey Index


Automatically created when you index a field that holds an array. MongoDB creates separate index entries
for each element of the array.

4.3.4 Text Index


Used to perform full-text search on string fields.
[Link]({ title: "text" })

4.4 Index Properties


4.4.1 Unique Index
A Unique Index prevents duplicate values for the indexed field. If you try to insert a document with a value
already present in the indexed field, MongoDB will reject the insertion with a duplicate key error.
[Link]({ name: 1 }, { unique: true }) // Now inserting two documents with
the same name will fail: // Error: E11000 duplicate key error

4.4.2 Partial Index


Introduced in MongoDB version 3.2. A Partial Index only indexes documents that match a specified filter
expression. This reduces storage and improves write performance compared to indexing all documents.
// Index only students in city "Indore" [Link]( { city: 1 }, {
partialFilterExpression: { city: "Indore" } } )

4.4.3 Sparse Index


A Sparse Index only contains entries for documents that have the indexed field. It skips documents
where the indexed field does not exist. This is useful when a field is optional — you avoid indexing
documents where the field is absent.
[Link]({ name: 1 }, { unique: true, sparse: true }) // Skips documents
where "name" field does not exist // Also enforces uniqueness for documents that DO have
"name"
4.5 Covered Queries
A Covered Query is a query where all fields used in the query are part of an index, AND all fields
returned in the result are in the same index. MongoDB can satisfy the entire query using only the index,
without reading any actual documents from disk — making it extremely fast.

Conditions for a Covered Query:

• All fields in the query filter are part of an index


• All fields returned (in projection) are part of the same index
// Create compound index [Link]({ type: 1, item: 1 }) // This query is
COVERED — uses only index data, no doc scan [Link]( { type: "game", item: /^c/
}, { item: 1, _id: 0 } // _id must be explicitly excluded )

An index CANNOT cover a query if any indexed field contains an array or a subdocument.

4.6 Limitations of Indexing


While indexes improve read performance, they have important trade-offs:

• Extra Storage: Each index requires additional disk space on the server.
• Slower Write Operations: Every INSERT, UPDATE, and DELETE must also update all relevant
indexes, adding overhead.
• Memory Usage: Indexes are stored in RAM. If total index size exceeds available RAM, performance
degrades.
• Over-Indexing Problem: Too many indexes can reduce overall efficiency — each write affects all
indexes.
• Maintenance Cost: Indexes need periodic optimization (rebuilding) for best performance.

4.7 Aggregation in MongoDB


Aggregation is an operation used to process data and return computed results. It groups data from
multiple documents and performs calculations to return a combined result. In SQL, this is equivalent to
COUNT(*) with GROUP BY.
Syntax: db.collection_name.aggregate(aggregate_operation) // Group books by type and
count each type [Link]([ { $group: { _id: "$type", count: { $sum: 1 } } } ])
// Output: { "_id": "ebook", "count": 2 } { "_id": "online", "count": 2 }

Expression Description

$sum Sums the specified values from all documents in the group

$avg Calculates the average value from all documents in the group

$min Returns the minimum value from all documents in the group

$max Returns the maximum value from all documents in the group

$push Inserts values into an array in the resulting document


Expression Description

$addToSet Inserts values to an array but avoids duplicates

$first Returns the first document from the source document group

$last Returns the last document from the source document group

4.8 MapReduce in MongoDB


MapReduce is a data processing paradigm for condensing large volumes of data into useful aggregated
results. It works in two phases:

• Map Phase: A JavaScript function emits key-value pairs from each document
• Reduce Phase: A JavaScript function aggregates the values for each unique key
MongoDB provides the mapReduce command to accomplish this:
[Link]( function() { emit(this.cust_id, [Link]); }, // Map
function(key, values) { return [Link](values); }, // Reduce { out: "order_totals" } //
Output collection )

MapReduce is used for complex analytics on large datasets. For simpler aggregation, the Aggregation
Pipeline is preferred.

4.9 Replication in MongoDB


Replication is the process of synchronizing data across multiple servers to ensure high availability and
data redundancy. MongoDB achieves replication through Replica Sets.

4.9.1 Why Replication?


• Data Safety — multiple copies prevent data loss from hardware failure
• High Availability (24×7) — no single point of failure
• Disaster Recovery — automatic failover to a secondary if primary fails
• No Downtime for Maintenance — index rebuilds, backups don't affect service
• Read Scaling — read requests can be distributed across secondary nodes
• Replica set is transparent to the application

4.9.2 How Replica Sets Work


A Replica Set is a group of mongod instances that maintain the same dataset. It always has exactly one
Primary node and one or more Secondary nodes.

• Primary Node: Receives all write operations. There can be only ONE primary.
• Secondary Nodes: Continuously replicate data from primary using an oplog (operation log).
• Arbiter: Optional node that only participates in elections, does not hold data.

If the primary node fails, the remaining members hold an election and one secondary is automatically
promoted to primary.
4.9.3 Setting Up a Replica Set — Step by Step
Step 1: Install MongoDB
Download and install MongoDB. Verify: mongod --version

Step 2: Create Data Directories


Create separate folders for each member: C:\mongo-replica\rs1 C:\mongo-replica\rs2
C:\mongo-replica\rs3

Step 3: Start Three MongoDB Instances


Terminal 1: mongod --port 27017 --dbpath C:\mongo-replica\rs1 --replSet myReplicaSet
Terminal 2: mongod --port 27018 --dbpath C:\mongo-replica\rs2 --replSet myReplicaSet
Terminal 3: mongod --port 27019 --dbpath C:\mongo-replica\rs3 --replSet myReplicaSet

Step 4: Connect to MongoDB Shell


mongosh --port 27017

Step 5: Initialize Replica Set


[Link]()

Step 6: Add Other Members


[Link]("localhost:27018") [Link]("localhost:27019")

Step 7: Check Status


[Link]()

Step 8: Test Replication


Insert on primary: [Link]({ name: "Alice", marks: 90 }) Check on
secondary: mongosh --port 27018 → [Link]()

4.10 Sharding in MongoDB


Sharding is the process of distributing data across multiple machines. It is MongoDB's approach to
horizontal scaling when a single server cannot handle the data size or read/write throughput demands.

4.10.1 Why Sharding?


• Single server has memory and disk limitations
• Vertical scaling (more CPU/RAM) is too expensive
• Replication alone cannot improve write performance (all writes go to primary)
• A single replica set is limited to 12 nodes
• Sharding provides unlimited horizontal scalability

4.10.2 Sharding Components


A MongoDB sharded cluster has three key components:

■ Shard
Each shard is a MongoDB instance (or replica set) that holds a subset of the data.
■ Mongos (Query Router)
Acts as the interface between client applications and the sharded cluster. Routes queries to the
appropriate shard(s).

■ Config Server
Stores metadata and configuration about the cluster — which data lives on which shard. Must be deployed
as a replica set.

4.10.3 How Sharding Works


Data is distributed across shards based on a shard key. The shard key is a field (or set of fields) that
MongoDB uses to partition data. MongoDB divides the data into chunks and distributes chunks evenly
across all shards. Applications always connect to Mongos — never directly to shard nodes.

4.11 Replication vs Sharding — Key Differences


Aspect Replication Sharding

Purpose High availability and data redundancy Horizontal scalability and load distribution

Data Storage All nodes have the same full dataset Each shard holds a different subset of data

Write Operations All writes go to primary node only Writes distributed across all shard nodes

Node Limit Limited to 12 nodes in a replica set No limit on number of shards

Read Scaling Secondaries can handle reads Both reads and writes scale with shards

Hardware Cost Requires high-end servers for large data Uses commodity servers for cost savings

Failure Handling Automatic failover via election Individual shards can use replica sets for HA

Best For Data safety and availability Very large datasets and high throughput

★ In production, MongoDB uses BOTH replication AND sharding: each shard is itself a replica
set.
UNIT 5 | PHP with MongoDB Integration

5.1 Setting Up PHP + MongoDB Environment


To integrate PHP with MongoDB, you need three components working together: a MongoDB server, a
PHP web server environment (XAMPP), and the MongoDB PHP driver.

Step 1: Install MongoDB Server


Download MongoDB Community Server from the official website. Run the installer → Choose
Complete Installation → Enable Install MongoDB as a Service. Verify: Open Command Prompt
and type: mongosh

Step 2: Install PHP Environment (XAMPP)


Download XAMPP from [Link] Install and start Apache from XAMPP
Control Panel. PHP will be available at [Link]

Step 3: Install MongoDB PHP Driver (DLL)


Go to: [Link] Download the DLL matching your PHP version
(e.g., php_mongodb.dll). Copy the DLL to: C:\xampp\php\ext\

Step 4: Enable MongoDB Extension in [Link]


Open: C:\xampp\php\[Link] Search for ;extension= and add the line:
extension=php_mongodb.dll Save the file.

Step 5: Restart Apache


Restart Apache in XAMPP Control Panel to load the MongoDB extension.

Step 6: Verify the Connection


Create C:\xampp\htdocs\[Link] with: <?php phpinfo(); ?> Open
[Link] in browser. Search for 'mongodb' on the page to confirm
the extension is loaded.

Step 7: Install MongoDB PHP Library via Composer


Download Composer from [Link] In Command Prompt run: composer
require mongodb/mongodb

5.2 CRUD Operations in PHP with MongoDB


CRUD stands for Create, Read, Update, Delete — the four fundamental database operations. Below are
complete PHP examples for each operation.

5.2.1 Connect to Database


<?php require 'vendor/[Link]'; // Composer autoloader $client = new
MongoDB\Client("mongodb://localhost:27017"); echo "Connection successful"; $db =
$client->examplesdb; // Select / create database echo "Database selected: examplesdb"; ?>
5.2.2 Create (Insert a Document)
<?php require 'vendor/[Link]'; $client = new
MongoDB\Client("mongodb://localhost:27017"); $collection =
$client->examplesdb->examplescol; $insertOneResult = $collection->insertOne([ 'title' =>
'MongoDB', 'description' => 'NoSQL Database', 'likes' => 100, 'url' =>
'[Link] ]); echo "Inserted ID: " .
$insertOneResult->getInsertedId(); ?>

5.2.3 Read (Find / Retrieve Documents)


<?php require 'vendor/[Link]'; $client = new
MongoDB\Client("mongodb://localhost:27017"); $collection =
$client->examplesdb->examplescol; // Find ALL documents $cursor = $collection->find();
foreach ($cursor as $document) { echo $document['title'] . "<br>"; } ?>

5.2.4 Filter Data (Find with Conditions)


<?php require 'vendor/[Link]'; $client = new
MongoDB\Client("mongodb://localhost:27017"); $collection =
$client->examplesdb->examplescol; // Find documents where likes > 50 $filter = ['likes'
=> ['$gt' => 50]]; $cursor = $collection->find($filter); foreach ($cursor as $document) {
echo $document['title'] . " - " . $document['likes'] . "<br>"; } ?>

5.2.5 Update (Modify a Document)


<?php require 'vendor/[Link]'; $client = new
MongoDB\Client("mongodb://localhost:27017"); $collection =
$client->examplesdb->examplescol; // Update the name field $collection->updateOne(
['name' => 'MongoDB'], // Filter ['$set' => ['name' => 'MongoDB Tutorial']] // Update );
echo "Document updated successfully"; // Display updated documents $cursor =
$collection->find(); foreach ($cursor as $document) { echo $document['name'] . "<br>"; }
?>

5.2.6 Delete (Remove a Document)


<?php require 'vendor/[Link]'; $client = new
MongoDB\Client("mongodb://localhost:27017"); $collection =
$client->examplesdb->examplescol; // Delete one document $collection->deleteOne(['name'
=> 'MongoDB Tutorial']); echo "Document deleted successfully"; // Verify deletion $cursor
= $collection->find(); foreach ($cursor as $document) { echo $document['name'] . "<br>";
} ?>

5.2.7 List All Databases


<?php require 'vendor/[Link]'; $client = new
MongoDB\Client("mongodb://localhost:27017"); foreach ($client->listDatabases() as
$dbInfo) { echo $dbInfo->getName() . "<br>"; } ?>

5.3 Sorting in PHP


Apply sort while finding documents in PHP:
<?php require 'vendor/[Link]'; $client = new
MongoDB\Client("mongodb://localhost:27017"); $collection =
$client->examplesdb->examplescol; // Sort by title ascending $options = ['sort' =>
['title' => 1]]; $cursor = $collection->find([], $options); foreach ($cursor as
$document) { echo $document['title'] . "<br>"; } ?>

5.4 Summary: CAP Theorem in Context of MongoDB


MongoDB is a CP system by default (Consistency + Partition Tolerance). In a replica set, all writes go to
the primary (ensuring consistency), and if a network partition occurs, MongoDB may become unavailable
rather than return stale data. However, MongoDB can be configured to be more AP-oriented by using read
preferences that allow reading from secondary nodes.

5.5 Quick Reference: All Key MongoDB Commands


Operation MongoDB Shell Command

Show databases show dbs

Create/switch database use dbName

Show collections show collections

Create collection (insert) [Link]({ key: value })

Insert one [Link]({ ... })

Insert many [Link]([{...}, {...}])

Find all [Link]()

Find with filter [Link]({ key: value })

Find with projection [Link]({}, { field: 1 })

Limit results [Link]().limit(5)

Skip results [Link]().skip(5)

Sort results [Link]().sort({ field: 1 })

Update one [Link]({ filter }, { $set: { ... } })

Delete one [Link]({ key: value })

Delete many [Link]({ key: value })

Delete all [Link]({})

Drop collection [Link]()

Drop database [Link]()

Create index [Link]({ field: 1 })


Operation MongoDB Shell Command

Get indexes [Link]()

Drop index [Link]({ field: 1 })

Aggregate [Link]([{ $group: { _id: "$field", count: { $sum: 1 } } }])

Replica set status [Link]()

Init replica set [Link]()

Add replica member [Link]("host:port")

Backup database mongodump --db dbName

Restore database mongorestore --db dbName dump/dbName


Exam Quick Revision — Key Points

NoSQL "Not Only SQL" — handles structured, semi-structured, and unstructured data at
scale.

CAP Theorem Consistency + Availability + Partition Tolerance → any distributed system can
guarantee at most 2 simultaneously.

MongoDB Data Model Database → Collection → Document (BSON/JSON key-value pairs)

_id field Every MongoDB document has a unique _id field (ObjectId by default —
auto-generated).

Schema-less MongoDB collections have no fixed schema; each document can have different fields.

insert() Inserts one or more documents. [Link]({...}) or [Link]([...])

find() Retrieves documents. Supports filters, projection, limit, skip, sort.

$set operator MUST use with update() to modify specific fields. Without $set, entire document is
replaced.

deleteOne vs deleteMany deleteOne() deletes first match. deleteMany() deletes all matches.

sort() values 1 = Ascending (A→Z, 0→9). -1 = Descending (Z→A, 9→0).

Projection 1/0 1 = Include field. 0 = Exclude field. Cannot mix (except _id).

Index Speeds up queries but slows down writes. Uses RAM. Over-indexing is harmful.

Unique Index Rejects duplicate values for the indexed field.

Partial Index Indexes only documents matching a filter expression. Added in v3.2.

Sparse Index Skips documents where the indexed field is absent.

Covered Query All query AND return fields are in one index → no document scan needed.

Aggregation Processes and returns computed results. Uses pipeline stages like $group, $match,
$sort.

MapReduce Two-phase processing (map + reduce) for complex analytics on large datasets.

Replica Set Group of mongod instances with same data. One Primary + N Secondaries.

Sharding Distributes data across multiple machines for horizontal scalability. Uses shard key.

Mongos Query router in sharding — client connects to mongos, never directly to shards.

mongodump Creates BSON backup of MongoDB data.

mongorestore Restores BSON data from a backup.

PHP connection new MongoDB\Client("mongodb://localhost:27017")

Embedded doc Faster reads, atomic updates, fixed size. Good when data always accessed together.
Referenced doc No duplication, flexible size. Use $lookup to join in aggregation.

All the best for your exams! — Study smart, understand concepts, practice MongoDB
commands.

You might also like