Following SQL & NoSQL
SQL Basics and Database Concepts
What is SQL?
SQL (Structured Query Language) is a standard programming language used to manage and manipulate relational databases. SQL enables you to create, retrieve, update, and delete data from databases.
Key Concepts in SQL
Database
A database is a collection of organized data, typically stored in tables, that can be easily accessed, managed, and updated.
Table
A table is a collection of related data stored in rows and columns. Each table is identified by a name, and each row in the table is a record containing data.
Record
A record is a row in a table, containing specific data for the columns (fields) in that table. For example, in the Customers table, each row represents a customer, with columns for CustomerID, CustomerName, ContactName, Address, etc.
Column
A column is a field in a table that holds a specific type of data (e.g., name, address, age).
Some of The Most Important SQL Commands
-- Step 1: Select the database
USE niteshsynergy;
-- Step 2: Create the Customers table
CREATE TABLE IF NOT EXISTS Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(100),
ContactName VARCHAR(100),
Address VARCHAR(150),
City VARCHAR(50),
PostalCode VARCHAR(20),
Country VARCHAR(50)
);
-- Step 3: Insert the data
INSERT INTO Customers (CustomerID, CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES
(1, 'Alfreds Futterkiste', 'Maria Anders', 'Obere Str. 57', 'Berlin', '12209', 'Germany'),
(2, 'Ana Trujillo Emparedados y helados', 'Ana Trujillo', 'Avda. de la Constitución 2222', 'México D.F.', '05021', 'Mexico'),
(3, 'Antonio Moreno Taquería', 'Antonio Moreno', 'Mataderos 2312', 'México D.F.', '05023', 'Mexico'),
(4, 'Around the Horn', 'Thomas Hardy', '120 Hanover Sq.', 'London', 'WA1 1DP', 'UK'),
(5, 'Berglunds snabbköp', 'Christina Berglund', 'Berguvsvägen 8', 'Luleå', 'S-958 22', 'Sweden');
to get the distinct count of countries from the Customers table is:
select count(distinct country) from customers;
Select all customers with a CustomerID greater than 80:
SELECT * FROM Customers
WHERE CustomerID > 80 ;
Here’s a detailed explanation of each operator that can be used in the WHERE clause with examples:
1. = (Equal)
The = operator is used to filter records that exactly match a specified value.
sql
SELECT * FROM Customers WHERE Country = 'Germany';
This will return all customers whose Country is exactly 'Germany'.
2. > (Greater than)
The > operator is used to find records where the value in a column is greater than the specified value.
sql
SELECT * FROM Customers WHERE CustomerID > 3;
This will return all customers with CustomerID greater than 3.
3. < (Less than)
The < operator is used to find records where the value in a column is less than the specified value.
sql
SELECT * FROM Customers WHERE CustomerID < 3;
This will return all customers with CustomerID less than 3.
4. >= (Greater than or equal)
The >= operator is used to find records where the value in a column is greater than or equal to a specified value.
sql
SELECT * FROM Customers WHERE CustomerID >= 3;
This will return all customers with CustomerID greater than or equal to 3.
5. <= (Less than or equal)
The <= operator is used to find records where the value in a column is less than or equal to a specified value.
sql
SELECT * FROM Customers WHERE PostalCode <= '05021';
This will return all customers with PostalCode less than or equal to '05021'.
6. <> or != (Not equal)
The <> (or != in some databases) operator is used to find records where the value in a column is not equal to the specified value.
sql
SELECT * FROM Customers WHERE Country <> 'Germany';
This will return all customers whose Country is not 'Germany'.
7. BETWEEN (Between a certain range)
The BETWEEN operator is used to find records where a column’s value falls within a specified range (inclusive).
sql
SELECT * FROM Customers WHERE CustomerID BETWEEN 3 AND 5;
This will return all customers with CustomerID values between 3 and 5 (inclusive).
8. LIKE (Search for a pattern)
The LIKE operator is used to search for a pattern in a column. It often works with wildcard characters:
sql
SELECT * FROM Customers WHERE CustomerName LIKE 'A%';
This will return all customers whose CustomerName starts with 'A'.
sql
SELECT * FROM Customers WHERE CustomerName LIKE '_r%';
This will return all customers whose CustomerName has 'r' as the second letter.
9. IN (To specify multiple possible values for a column)
The IN operator is used to specify a list of possible values for a column.
sql
SELECT * FROM Customers WHERE Country IN ('Germany', 'Mexico', 'Sweden');
This will return all customers whose Country is either 'Germany', 'Mexico', or 'Sweden'.
Summary
The SQL ORDER BY
The ORDER BY
keyword is used to sort the result-set in ascending or descending order.
SELECT * FROM Products
ORDER BY Price;
SELECT * FROM Products
ORDER BY Price DESC ;
select * from products order by price asc, ProductName desc;
select * from customers where country='Mexico' and customerName like 'a%';
1. Fetching the nth record from the top:
For nth record from the top (generalized):
For MySQL and PostgreSQL (which supports LIMIT and OFFSET): sql Copy code
SELECT * FROM Products
ORDER BY ProductID
LIMIT 1 OFFSET n-1;
For Oracle (which does not support LIMIT and OFFSET, but uses ROWNUM or FETCH):
SELECT * FROM (
SELECT * FROM Products
ORDER BY ProductID
)
WHERE ROWNUM = n;
Replace n with the desired record number.
Note: Alternatively, for Oracle 12c and above (with FETCH
):
SELECT * FROM Products
ORDER BY ProductID
FETCH NEXT 1 ROWS ONLY OFFSET n-1 ROWS;
For nth record from the bottom (generalized):
For MySQL and PostgreSQL:
SELECT * FROM Products
ORDER BY ProductID DESC
LIMIT 1 OFFSET n-1;
For Oracle: Using a subquery to reverse the order (same as for fetching from the top, but with DESC order):
SELECT * FROM (
SELECT * FROM Products
ORDER BY ProductID DESC
)
WHERE ROWNUM = n;
Note:Alternatively, for Oracle 12c and above (with FETCH):
SELECT * FROM Products
ORDER BY ProductID DESC
FETCH NEXT 1 ROWS ONLY OFFSET n-1 ROWS;
DDL is used to define and manage database structures such as tables, schemas, and indexes. These commands modify the structure of the database.
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT
);
ALTER: Modifies an existing database object, such as adding, deleting, or modifying columns in a table.
ALTER TABLE employees ADD COLUMN email VARCHAR(100);
DROP: Deletes an existing database object (e.g., table, view).
DROP TABLE employees;
TRUNCATE: Removes all rows from a table, but retains the table structure for future use.
TRUNCATE TABLE employees;
RENAME (Oracle 9i): Used to rename an existing database object such as a table or column.
RENAME employees TO staff;
DML is used to manipulate data in existing tables. These commands deal with the insertion, update, deletion, and merging of data.
INSERT INTO employees (id, name, age) VALUES (1, 'John Doe', 30);
UPDATE: Modifies existing records in a table.
UPDATE employees SET age = 31 WHERE id = 1;
DELETE: Removes records from a table.
DELETE FROM employees WHERE id = 1;
MERGE (Oracle 9i): A combination of INSERT, UPDATE, and DELETE used to merge data into a table. It's often used for upserting (inserting or updating).
MERGE INTO employees e
USING new_employees ne
ON (e.id = ne.id)
WHEN MATCHED THEN
UPDATE SET e.name = ne.name, e.age = ne.age
WHEN NOT MATCHED THEN
INSERT (id, name, age) VALUES (ne.id, ne.name, ne.age);
DQL is used for querying the database to retrieve data. The most common command is SELECT.
SELECT name, age FROM employees WHERE age > 30;
TCL is used to manage transactions in the database. These commands ensure that all the operations within a transaction are completed successfully or rolled back if there’s an error, maintaining data consistency.
COMMIT;
ROLLBACK: Rolls back the current transaction, undoing any changes made.
ROLLBACK;
SAVEPOINT: Sets a savepoint within a transaction to which you can later roll back if needed.
SAVEPOINT savepoint_name;
DCL is used to control access to data and database objects. These commands manage user permissions.
GRANT SELECT, INSERT ON employees TO user1;
REVOKE: Removes a user's privileges to perform certain actions on a database object.
REVOKE INSERT ON employees FROM user1;
The ACID properties:
The ACID properties (Atomicity, Consistency, Isolation, Durability) are fundamental principles in database management systems (DBMS) to ensure that transactions are processed reliably. Here's a real-time example for each property, along with a use case:
Use Case: Banking Transfer
In summary:
These properties are crucial for ensuring reliable and correct database transactions in systems like banking, e-commerce, reservation platforms, and more.
Here’s how you can achieve each of the ACID properties in Java with examples for each:
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class BankService {
private final AccountRepository accountRepository;
public BankService(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
@Transactional
public void transferFunds(Long senderId, Long receiverId, double amount) {
// Deduct from sender's account
Account sender = accountRepository.findById(senderId).orElseThrow(() -> new RuntimeException("Sender not found"));
sender.setBalance(sender.getBalance() - amount);
accountRepository.save(sender);
// Simulate an error: Uncomment the line below to trigger rollback
if (amount > 1000) throw new RuntimeException("Simulated error");
// Add to receiver's account
Account receiver = accountRepository.findById(receiverId).orElseThrow(() -> new RuntimeException("Receiver not found"));
receiver.setBalance(receiver.getBalance() + amount);
accountRepository.save(receiver);
}
}
@Transactional ensures that if an exception occurs, the whole transaction will be rolled back, ensuring atomicity.
Consistency ensures that the database constraints and business logic are always maintained.
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class InventoryService {
private final ProductRepository productRepository;
public InventoryService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
@Transactional
public void purchaseProduct(Long productId, int quantity) {
Product product = productRepository.findById(productId)
.orElseThrow(() -> new RuntimeException("Product not found"));
if (product.getStock() < quantity) {
throw new RuntimeException("Insufficient stock available");
}
product.setStock(product.getStock() - quantity);
productRepository.save(product);
}
}
The purchaseProduct
method ensures that if there is insufficient stock, a runtime exception is thrown, maintaining consistency. The @Transactional
annotation will ensure that any operation is atomic and consistent.
Isolation ensures that one transaction is not affected by another transaction, which is critical in multi-user environments.
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ReservationService {
private final ReservationRepository reservationRepository;
public ReservationService(ReservationRepository reservationRepository) {
this.reservationRepository = reservationRepository;
}
@Transactional
public void bookSeat(Long seatId, Long customerId) {
// Fetch seat and customer details
Seat seat = reservationRepository.findSeatById(seatId);
if (seat.isBooked()) {
throw new RuntimeException("Seat is already booked");
}
seat.setBooked(true);
reservationRepository.save(seat);
// Log customer booking
reservationRepository.saveBookingLog(customerId, seatId);
}
}@Transactional
ensures that while the seat is being booked, the operation is isolated from other transactions that might also be booking seats simultaneously.
Once a transaction is committed, it should be saved permanently in the database even if the system crashes afterward.
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class OrderService {
private final OrderRepository orderRepository;
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
@Transactional
public void placeOrder(Long customerId, List<Long> productIds) {
// Place the order and save to database
Order order = new Order();
order.setCustomerId(customerId);
order.setProductIds(productIds);
order.setStatus("PLACED");
orderRepository.save(order);
// After the commit, this data should persist even if the system crashes
}
}
With @Transactional
, the changes made to the database will persist even if the system crashes after the commit. Once the transaction is committed, it’s durable.
In real-time enterprise applications, these concepts are implemented using frameworks like Spring Transaction Management or JDBC (for lower-level control), making the system robust and reliable.
Common ones:
MIN() – smallest value in a column
MAX() – largest value
COUNT() – total rows
SUM() – total of numeric values
AVG() – average of numeric values
Ignore NULL values (except COUNT(*)).
Syntax:
SELECT
MIN(col), MAX(col), COUNT(*), SUM(num_col), AVG(num_col)
FROM table_name;
Eg-
Facebook‑style use case:
– Count posts per user:
SELECT user_id, COUNT(*) AS total_posts
FROM posts
GROUP BY user_id;
– Average friends per user:
SELECT user_id, AVG(friend_count) AS avg_friends
FROM users
GROUP BY user_id;
2. GROUP BY
Concept:
Splits rows into groups by one or more columns.
Typically paired with aggregate functions.
Syntax:
SELECT col1, col2, AGG(col3)
FROM table
GROUP BY col1, col2;
Use case on Facebook:
Posts per day per region:
SELECT region, DATE(created_at) AS day, COUNT(*) AS posts
FROM posts
GROUP BY region, day;
3. HAVING
Concept:
Filters results after aggregation (unlike WHERE).
Useful to apply conditions on aggregate values.
Syntax:
SELECT col, AGG(col2)
FROM table
GROUP BY col
HAVING AGG(col2) > value;
Facebook‑style use case:
Users with more than 100 friends:
SELECT user_id, COUNT(friend_id) AS num_friends
FROM friendships
GROUP BY user_id
HAVING COUNT(friend_id) > 100;
1. SQL JOINs
Concept (Point by Point)
Combines rows from two or more tables based on related columns.
Most used in relational databases to query across relationships.
JOINs depend on primary & foreign key relationships.
Types of JOIN:
INNER JOIN: Only matched rows.
LEFT JOIN: All left + matched right.
RIGHT JOIN: All right + matched left.
FULL JOIN: All left + all right.
CROSS JOIN: Cartesian product (every row with every other).
SELF JOIN: Join table with itself.
Syntax
SELECT a.column1, b.column2
FROM table1 a
JOIN table2 b ON a.common_column = b.common_column;
eg-
-- Tables
Users(user_id, name)
Posts(post_id, user_id, content)
-- Query: List all users and their posts
SELECT u.name, p.content
FROM Users u
JOIN Posts p ON u.user_id = p.user_id;
eg-Fetch all comments along with the user names who posted them.
SELECT u.name, c.comment_text
FROM Users u
JOIN Comments c ON u.user_id = c.user_id;
2. SQL GROUP BY
Concept (Point by Point)
Aggregates data based on one or more columns.
Used with aggregate functions: COUNT, SUM, AVG, MIN, MAX.
Groups similar values together.
Must come after WHERE and before ORDER BY.
Syntax:
SELECT column, COUNT(*)
FROM table
GROUP BY column;
-- Count how many posts each user has made
SELECT user_id, COUNT(*) AS post_count
FROM Posts
GROUP BY user_id;
eg-How many friends each user has?
SELECT user_id, COUNT(friend_id) AS total_friends
FROM Friendships
GROUP BY user_id;
3. SQL INDEX
Concept (Point by Point)
Improves query performance (esp. SELECT).
Works like an index in a book — fast lookup.
Created on frequently searched columns or JOIN keys.
But slows down INSERT/UPDATE/DELETE due to maintenance.
🧪 Syntax
-- Create index
CREATE INDEX idx_user_name ON Users(name);
-- Drop index
DROP INDEX idx_user_name;
eg-
-- Index on email to speed up login lookup
CREATE INDEX idx_user_email ON Users(email);
eg-Quickly find user by email or username for login
SELECT * FROM Users WHERE email = 'mark@fb.com';
Combine: JOIN + GROUP BY + Function Example
-- Get each user's total number of comments
SELECT u.name, COUNT(c.comment_id) AS comment_count
FROM Users u
JOIN Comments c ON u.user_id = c.user_id
GROUP BY u.name;
1️⃣ WHERE vs HAVING
🔹 WHERE: filters rows before aggregation
🔹 HAVING: filters groups after GROUP BY
-- Users with posts > 5
SELECT user_id, COUNT(*) AS total_posts
FROM Posts
GROUP BY user_id
HAVING COUNT(*) > 5;
📌 Use Case: Show only users who made more than 5 posts.
2️⃣ Subqueries
🔹 Query inside another query
🔹 Can be used in SELECT, WHERE, or FROM
🔹 Types: scalar, correlated, nested
-- Get users with highest post count
SELECT name FROM Users
WHERE user_id = (
SELECT user_id FROM Posts
GROUP BY user_id
ORDER BY COUNT(*) DESC LIMIT 1
);
📌 Use Case: Facebook - Who made the most posts?
3️⃣ Window Functions
🔹 Perform ranking, running totals, etc.
🔹 Do not collapse rows like GROUP BY
SELECT user_id, post_id,
RANK() OVER (PARTITION BY user_id ORDER BY created_at DESC) AS recent_rank
FROM Posts;
📌 Use Case: Get latest post per user (or Nth salary-type problems)
4️⃣ UNION vs UNION ALL
🔹 UNION: Removes duplicates
🔹 UNION ALL: Keeps all rows
SELECT name FROM Customers
UNION
SELECT name FROM Vendors;
📌 Use Case: Combine all names from two different sources.
5️⃣ CASE WHEN
🔹 Conditional logic in SQL
🔹 Like if-else
SELECT name,
CASE
WHEN gender = 'M' THEN 'Male'
WHEN gender = 'F' THEN 'Female'
ELSE 'Other'
END AS gender_text
FROM Users;
📌 Use Case: Facebook profile – show readable gender labels.
6️⃣ Views
🔹 Saved SQL query (virtual table)
🔹 Helps simplify complex queries
CREATE VIEW ActiveUsers AS
SELECT * FROM Users WHERE status = 'active';
📌 Use Case: Admin panel can use ActiveUsers as a table.
7️⃣ CTE (Common Table Expression)
🔹 Temporary result set
🔹 Declared using WITH keyword
🔹 Cleaner than subqueries; can be recursive
WITH PostCounts AS (
SELECT user_id, COUNT(*) AS total_posts
FROM Posts GROUP BY user_id
)
SELECT u.name, pc.total_posts
FROM Users u
JOIN PostCounts pc ON u.user_id = pc.user_id;
📌 Use Case: Modularize complex queries for readability.
8️⃣ Constraints
🔹 Enforce rules at DB level
🔹 Types:
PRIMARY KEY – Unique identifier
FOREIGN KEY – Link to another table
UNIQUE, NOT NULL, CHECK, DEFAULT
CREATE TABLE Users (
user_id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE NOT NULL,
age INT CHECK(age >= 13)
);
📌 Use Case: Block underage signup, prevent duplicate emails.
SQL Assignment
Visit:→ https://github.com/niteshsynergy/sqlpractice/blob/main/sqlpractice
Follow
Level | Focus Area | No. of Qs | Example |
---|---|---|---|
✅ Beginner | SELECT, WHERE, ORDER, LIMIT, LIKE | 100 | SELECT * FROM users WHERE full_name LIKE 'A%'; |
✅ Intermediate | JOINs, Aggregates, GROUP BY, HAVING | 150 | Get users with more than 5 posts. |
✅ Advanced | Subqueries, EXISTS, IN, Nested SELECTs | 150 | Users who never posted but liked a post. |
✅ Expert | CTEs, Window Functions, Ranking | 150 | Rank users by number of followers. |
✅ Real-World Logic | Use-case logic, multi-step JOINs | 150 | Find top 3 most followed users who posted in last 7 days. |
✅ Performance | Query optimization, Indexing practice | 50 | Explain slow queries and improve with indexes. |
✅ Admin/DDL | CREATE, ALTER, DROP, TRIGGERS | 100 | Add a trigger to log deleted comments. |
✅ Transactions | COMMIT, ROLLBACK, Isolation Levels | 50 | Simulate transfer between users. |
✅ JSON/Date | JSON fields, time-based logic | 50 | Fetch posts with images added in last 30 days. |
Your encouragement keeps us going!
If you find value in our content, please consider supporting us.
💡 Even a small contribution can make a big difference in helping us build better educational resources.
MongoDB
MongoDB is a NoSQL, document-oriented database that stores data in flexible, JSON-like format (BSON). It's known for scalability, high performance, and flexible schema, making it ideal for handling large volumes of unstructured data.
MongoDB is a powerful NoSQL database that offers a range of features designed to provide flexibility, scalability, and high performance. Below are some of the most important features of MongoDB, merging the points from both sources:
Databases can be divided in 3 types:
RDBMS (Relational Database Management System)
OLAP (Online Analytical Processing)
NoSQL (recently developed database)
NoSQL Database is used to refer a non-SQL or non relational database.
It provides a mechanism for storage and retrieval of data other than tabular relations model used in relational databases. NoSQL database doesn't use tables for storing data. It is generally used to store big data and real-time web applications.
It supports query language.
It provides fast performance.
It provides horizontal scalability.
MongoDB Data Types: Concepts, Use Cases, and Real-Time Example with Code Snippets:
Code Snippet:
db.blogposts.insertOne({
title: "MongoDB Tutorial",
body: "Learn how to use MongoDB effectively."
});
Code Snippet:
db.products.insertOne({
name: "Laptop",
stock: 50
});
Code Snippet:
db.tasks.insertOne({
task: "Complete MongoDB tutorial",
completed: false
});
Code Snippet:
db.orders.insertOne({
orderId: 12345,
orderDate: new Date("2024-11-28T10:00:00Z")
});
Code Snippet:
const userId = new ObjectId(); // Generate a unique ObjectId
db.users.insertOne({
_id: userId,
name: "John Doe",
email: "johndoe@example.com"
});
Code Snippet:
db.products.insertOne({
name: "T-shirt",
tags: ["cotton", "red", "medium"]
});
Code Snippet:
db.users.insertOne({
name: "Jane Smith",
address: {
street: "123 Elm St",
city: "Springfield",
postalCode: "12345"
}
});
Code Snippet:
db.products.insertOne({
name: "Smartphone",
discountedPrice: null
});
Code Snippet:
// Store binary data (e.g., an image) in GridFS
var fs = require('fs');
var file = fs.readFileSync('path/to/image.jpg');
db.fs.files.insertOne({
filename: "image.jpg",
contentType: "image/jpeg",
file: file
});
Code Snippet:
const Decimal128 = require('mongodb').Decimal128;
db.products.insertOne({
name: "Laptop",
price: Decimal128.fromString("1299.99")
});
Code Snippet:
db.users.insertOne({
name: "Alice",
lastLogin: new Timestamp()
});
For more details or study visit → https://www.openmymind.net/mongodb.pdf
___________________________________________________________________________________
✅ Module 1: Installation of MongoDB
Step-by-step:
To test:
mongod # runs the MongoDB server
In a new terminal:
mongo # opens the shell (MongoDB CLI)
⚠️ On Windows, you may need to add MongoDB to your system PATH.
0.0.0.0/0
(allows connection from anywhere)
use niteshsynergyDB // creates or switches to niteshsynergyDB
db.createCollection("users") // creates collection
db.users.insertOne({
_id: "u101",
name: "Nitesh",
age: 29,
country: "India"
})
db.users.find().pretty()
practiceDB
students
{
"rollNo": 1,
"name": "Amit",
"marks": 75
}
✅ Module 2: Basic CRUD Operations in MongoDB
// Insert one document
db.students.insertOne({
rollNo: 1,
name: "Amit",
marks: 75
})
// Insert multiple documents
db.students.insertMany([
{ rollNo: 2, name: "Nitesh", marks: 88 },
{ rollNo: 3, name: "Sneha", marks: 92 }
])
// Find all documents
db.students.find()
// Find with condition
db.students.find({ name: "Nitesh" })
// Pretty print
db.students.find().pretty()
// Find one
db.students.findOne({ rollNo: 2 })
// Update one document
db.students.updateOne(
{ rollNo: 2 },
{ $set: { marks: 90 } }
)
// Update many documents
db.students.updateMany(
{ marks: { $lt: 80 } },
{ $set: { status: "Needs Improvement" } }
)
// Delete one document
db.students.deleteOne({ rollNo: 3 })
// Delete many documents
db.students.deleteMany({ status: "Needs Improvement" })
Try these steps in MongoDB shell or Compass:
practiceDB
:use practiceDB
rollNo
, name
, marks
, grade
.
✅ Module 3: Query Operators in MongoDB (Filtering & Search)
$gt
, $lt
, $in
, $and
, and $or
work.
Assume your collection has these student documents:
{ "rollNo": 1, "name": "Amit", "marks": 75 }
{ "rollNo": 2, "name": "Nitesh", "marks": 88 }
{ "rollNo": 3, "name": "Sneha", "marks": 92 }
{ "rollNo": 4, "name": "Rahul", "marks": 67 }
Operator | Meaning | Example |
---|---|---|
$gt | Greater than | { marks: { $gt: 80 } } |
$lt | Less than | { marks: { $lt: 70 } } |
$gte | Greater than or equal | { marks: { $gte: 88 } } |
$lte | Less than or equal | { marks: { $lte: 75 } } |
$ne | Not equal | { name: { $ne: "Nitesh" } } |
$in | Matches any in array | { name: { $in: ["Amit", "Sneha"] } } |
// AND - both conditions must be true
db.students.find({
$and: [{ marks: { $gt: 70 } }, { marks: { $lt: 90 } }]
})
// OR - either condition must be true
db.students.find({
$or: [{ marks: { $lt: 70 } }, { marks: { $gt: 90 } }]
})
Suppose this document exists:
{
"rollNo": 5,
"name": "Anjali",
"marks": 85,
"skills": ["Java", "MongoDB", "Spring Boot"]
}
You can search inside arrays:
db.students.find({ skills: "MongoDB" }) // exact match in array
db.students.find({ skills: { $in: ["React", "MongoDB"] } })
Embed when you have one-to-few relationships or data that's always read together.
{
"_id": 1,
"name": "Nitesh",
"email": "nitesh@example.com",
"address": {
"street": "MG Road",
"city": "Patna",
"zip": "800001"
}
}
Reference when you have one-to-many or many-to-many relationships.
customers
collection
{
"_id": "cust101",
"name": "Amit Kumar"
}
orders
collection{
"_id": "order789",
"custId": "cust101",
"amount": 450.0,
"items": ["item1", "item2"]
}
To fetch related orders, use:
db.orders.find({ custId: "cust101" })
✅ In aggregation:
$lookup
works like a JOIN.
Case | Best Approach |
---|---|
User profile with address | Embed |
Blog post and comments | Embed (few) or Ref |
Customer and multiple orders | Reference |
Category and product | Reference |
Invoice with product snapshots | Embed (snapshot) |
$match
, $group
, $project
, $sort
, $lookup
, etc.
Aggregation is like SQL’s GROUP BY
, JOIN
, or even Excel's Pivot Table. It lets you process and analyze large datasets in real time.
Each stage is like a step in a conveyor belt:
db.collection.aggregate([
{ stage1 },
{ stage2 },
{ stage3 }
])
$match
— Filter Documentsdb.students.aggregate([
{ $match: { marks: { $gt: 80 } } }
])
$group
— Group & Aggregate Datadb.students.aggregate([
{
$group: {
_id: "$grade", // group by grade
totalMarks: { $sum: "$marks" },
avgMarks: { $avg: "$marks" }
}
}
])
$project
— Shape Outputdb.students.aggregate([
{
$project: {
name: 1,
marks: 1,
percentage: { $divide: ["$marks", 100] }
}
}
])
4️⃣ $sort
— Sort Results
db.students.aggregate([
{ $sort: { marks: -1 } } // descending
])
$lookup
— Join Collections (Reference)Join orders
with customers
:
db.orders.aggregate([
{
$lookup: {
from: "customers",
localField: "custId",
foreignField: "_id",
as: "customerDetails"
}
}
])
✅ Module 6: Indexing, Performance & Explain Plans in MongoDB
explain()
method.
An index in MongoDB is like an index in a book — it helps the database find data faster, without scanning every document.
📌 Default index: Every
_id
field in MongoDB has an index automatically.
db.collection.createIndex({ fieldName: 1 }) // ascending
db.collection.createIndex({ fieldName: -1 }) // descending
db.students.createIndex({ name: 1 }) // index on 'name'
An index on multiple fields:
db.students.createIndex({ name: 1, marks: -1 })
Use when your query filters/sorts on multiple fields.
_____________________________________________________________________
db.students.dropIndex({ name: 1 })
To drop all:
db.students.dropIndexes()
🛠️ 5. Viewing Indexes
db.students.getIndexes()
🔍 6. Analyze Query with explain()
db.students.find({ name: "Nitesh" }).explain("executionStats")
Look for:
IXSCAN
means index used, COLLSCAN
means full collection scan (slow).
✅ Use indexes on:
.find()
filters.sort()
$lookup
joins❌ Don’t overuse — too many indexes slow down writes.
Change Streams allow applications to watch for changes in the database and react to them in real time, like triggers in relational databases.
const changeStream = db.collection.watch();
changeStream.on("change", (change) => {
console.log(change);
});
2. Using Filters
You can filter change streams for specific operations (e.g., only updates or deletions).
const changeStream = db.collection.watch([
{ $match: { operationType: "update" } }
]);
Handling Change Streams in Production
MongoDB Atlas is MongoDB’s fully managed cloud service for deploying, managing, and scaling MongoDB clusters in the cloud.
RBAC in MongoDB allows you to define and enforce who can access the database and what actions they can perform (read, write, admin).
Create User with Specific Roles:
db.createUser({
user: "myUser",
pwd: "myPassword",
roles: [ { role: "readWrite", db: "test" } ]
});
2. Built-in Roles:
read
: Can only read.readWrite
: Can read and write.dbAdmin
: Can manage databases.root
: Admin privileges for all databases.Sharding distributes your data across multiple machines (or shards), which helps manage large datasets and high-traffic apps.
userId
), which evenly distributes the data.
Geospatial queries help you find locations within a given radius or near other geographical points, like GPS coordinates.
x, y
coordinates.longitude
, latitude
) in a locations
collection.Run queries like:
db.locations.find({
location: {
$near: {
$geometry: { type: "Point", coordinates: [ -73.97, 40.77 ] },
$maxDistance: 5000
}
}
});
→ End….
Your encouragement keeps us going!
If you find value in our content, please consider supporting us.
💡 Even a small contribution can make a big difference in helping us build better educational resources.
Donate Now
Follow us on Social media like X, Facebook, Instagram, YouTube & other platform where you find us!
Data Integrity, Data Security, and Data Indexing:
In a Java Spring Boot application, you can achieve Data Integrity, Data Security, and Data Indexing in various ways using Spring and related technologies. Here's a breakdown of how each can be achieved:
In a Java Spring Boot application, you can achieve Data Integrity, Data Security, and Data Indexing in various ways using Spring and related technologies. Here's a breakdown of how each can be achieved:
Data integrity refers to the accuracy, consistency, and reliability of data throughout its lifecycle. This ensures that data is not corrupted or lost and adheres to defined rules and constraints.
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull(message = "Product name cannot be null")
private String name;
@Positive(message = "Price must be positive")
private double price;
@NotNull(message = "Stock cannot be null")
private Integer stock;
}
Spring Boot Validation Example:
import org.springframework.stereotype.Service;
import javax.validation.Valid;
@Service
public class ProductService {
private final ProductRepository productRepository;
public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
public void addProduct(@Valid Product product) {
productRepository.save(product); // Will automatically validate the product before saving
}
}
@NotNull
, @Size
, @Email
help ensure integrity before the entity is persisted to the database.@Transactional
.Data security ensures that sensitive data is protected from unauthorized access or tampering. This includes data encryption, authentication, authorization, and secure transmission.
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin();
}
}
Encrypting Sensitive Data (e.g., passwords or credit card numbers):
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Service
public class UserService {
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public void registerUser(String username, String plainPassword) {
String encodedPassword = passwordEncoder.encode(plainPassword);
// Store encodedPassword in the database
}
}
JWT for Token-Based Authentication:
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
public class JwtTokenUtil {
private String secretKey = "mySecretKey"; // Secret key used to sign the JWT
public String generateToken(String username) {
return Jwts.builder()
.setSubject(username)
.signWith(SignatureAlgorithm.HS256, secretKey)
.compact();
}
}
Transport Layer Security (TLS): Ensure data is transmitted securely by enabling HTTPS for all sensitive API requests in Spring Boot.
Data indexing improves the performance of database queries by allowing fast lookups based on indexed fields.
@Indexed
(Spring Data JPA, Hibernate).
import javax.persistence.*;
import org.hibernate.search.annotations.Indexed;
@Entity
@Indexed
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String name;
@Column(nullable = false)
private double price;
@Indexed
@Column(nullable = false)
private String category;
}
In this example:
@Indexed
annotation marks the category column to be indexed, improving search performance on this column.pom.xml
file:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
Then configure and use ElasticsearchRepository for efficient querying:
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ProductRepository extends ElasticsearchRepository<Product, Long> {
List<Product> findByCategory(String category);
}
@NotNull
, @Positive
, and @Size
, along with transactional management for atomic operations.@Indexed
for faster queries, or use Elasticsearch for high-performance search and indexing.With Spring Boot, these practices can be easily integrated into your application, ensuring reliable, secure, and high-performing systems.