LU 3 and LU 4 NoSQL
LU 3 and LU 4 NoSQL
Data Models:
Document Model: MongoDB uses JSON-like documents for flexible schema design.
Embedding vs. Referencing:
o Embedding: Store related data within a single document. Ideal for one-to-one
and one-to-many relationships with small datasets.
o Referencing: Use references (ObjectIDs) for large datasets or many-to-many
relationships.
Understanding Requirements:
o Identify entities, attributes, and relationships from the application requirements.
o Model data according to MongoDB's schema-less design principles (document-
oriented model).
Data Definition Tasks:
o Create Databases: Use use <database_name> to create or switch to a database.
o Create Collections: Collections are automatically created when you insert the
first document, or you can explicitly define collections with settings like capped
collections
Syntax:
Schema Validation:
db.createCollection("example", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["field1", "field2"],
properties: {
field1: { bsonType: "string" },
field2: { bsonType: "number" }
}
}
}
});
CRUD Operations:
Create:
Read:
Update:
Delete:
db.collection.bulkWrite([
]);
Insert Data:
o Insert one or multiple documents into a collection.
Read Data:
Retrieve data using queries.
db.collection.find({ field1: "value1" }) // Filtered query
db.collection.find().sort({ field1: 1 }) // Sorted query
Update Data:
Modify existing documents with update or replace operations.
Delete Data:
Use Indexes:
o Ensure fields used in frequent queries are indexed.
Aggregation Framework:
o Use the aggregation pipeline for complex queries.
db.collection.aggregate([
{ $match: { field1: "value1" } },
{ $group: { _id: "$field2", total: { $sum: 1 } } }
])
Role-Based Access Control (RBAC): Assign roles with specific privileges to users.
Examples:
o read: Allows reading data.
o readWrite: Allows reading and writing data.
o dbAdmin: Database administration tasks.
Create Users:
o Assign roles and permissions to users.
Syntax:
db.createUser({
user: "username",
pwd: "password",
roles: [{ role: "readWrite", db: "databaseName" }]
})
Update Roles:
o Modify user roles and permissions when necessary.
Audit User Actions:
o Track user activities for better accountability.
Authentication:
Encryption:
TLS (Transport Layer Security) and its predecessor SSL (Secure Sockets Layer) are
cryptographic protocols designed to secure communication over a network. They are primarily
used to establish a secure and encrypted connection between a client (e.g., a browser) and a
server (e.g., a website).
Assign roles like read, readWrite, and dbAdmin based on the principle of least privilege.
Network Security:
A VPN (Virtual Private Network) is a technology that creates a secure and encrypted
connection over a less secure network, such as the internet. It allows users to send and
receive data as if their devices were directly connected to a private network, ensuring
privacy, security, and anonymity.
Answer:
Answer:
Answer:
1. Create an Index:
4. Adjust Indexes: If necessary, create a compound index to further optimize for additional fields used in
queries.
LU 4: Manage Database
Answer:
db.createUser({
user: "ecom_user",
pwd: "securepassword123",
roles: [
});
Answer:
Best Practices:
1. Enable authentication.
2. Use TLS/SSL for secure connections.
3. Restrict network access using IP whitelisting.
security:
authorization: "enabled"
Answer:
1. Steps to Deploy:
o Use replica sets for high availability.
o Configure sharding for scalability (if needed).
o Deploy on a secure server with limited access.
2. Monitoring:
3. Backup:
Objective:
Develop a NoSQL database to manage a library's book inventory, user details, and borrowing
records using a document-based database like MongoDB.
Entities:
1. Books: Store details such as title, author, genre, publication year, and availability
status.
2. Users: Store details like name, contact information, and the list of borrowed
books.
3. Transactions: Store borrowing and returning history.
Functionalities:
1. Books:
{
"_id": "unique_book_id",
"title": "Book Title",
"author": "Author Name",
"genre": "Genre",
"publication_year": 2020,
"available": true
}
2. Users:
{
"_id": "unique_user_id",
"name": "User Name",
"contact": {
"email": "user@example.com",
"phone": "1234567890"
},
"borrowed_books": [
{
"book_id": "unique_book_id",
"borrowed_date": "2025-01-01",
"due_date": "2025-01-15"
}
]
}
3. Transactions:
{
"_id": "unique_transaction_id",
"user_id": "unique_user_id",
"book_id": "unique_book_id",
"transaction_type": "borrow" or "return",
"date": "2025-01-01"
}
Basic Queries
Advanced Queries
1. Add books/users.
2. Update records (e.g., when a user borrows a book).
3. Delete records (e.g., remove a book from inventory).
4. Read data (e.g., get the list of available books).
Deliverables:
Here's a practical exercise to develop and work with a NoSQL database. The goal is to create a
small NoSQL-based application, using MongoDB (a popular NoSQL database). This exercise
will help you learn how to design, implement, and query a NoSQL database.
Practice Work 2: Build a NoSQL Movie Catalog
Scenario:
You are tasked with creating a movie catalog application. The catalog will store information
about movies, their genres, directors, and user reviews.
Steps to Follow:
1. Install MongoDB
NoSQL databases use collections (similar to tables) and documents (similar to rows). Design a
schema for your movie catalog:
Movies Collection:
"title": "Inception",
"release_year": 2010,
"ratings": [
}
3. Set Up the Environment
use movie_catalog
db.movies.insertMany([
"title": "Inception",
"release_year": 2010,
"ratings": [
},
"release_year": 2008,
"genres": ["Action", "Crime", "Drama"],
"ratings": [
])
db.movies.find()
6. Update Data
db.movies.updateOne(
{"title": "Inception"},
{$addToSet: {"genres": "Mystery"}}
7. Delete Data
Use a programming language like Python (with pymongo) or Node.js (with mongodb package) to
interact with your MongoDB database. Create a simple app where users can:
Outcome
Here are five practical tasks or integrated situations related to developing NoSQL
databases that are often used in exams. Each task includes a description and guidance on
how to approach it.
Scenario:
A company needs a database for an e-commerce platform. Each product has a unique ID, name,
price, category, description, and a list of reviews (each with a user ID, rating, and comment).
Steps to Solve:
{
"productID": "12345",
"name": "Laptop",
"price": 1200,
"category": "Electronics",
"reviews": [
Use MongoDB queries to insert, update, delete, and fetch product data.
Scenario:
A social media application uses a NoSQL database to store user posts. The database needs to
efficiently retrieve all posts by a specific user, ordered by timestamp.
Steps to Solve:
Test the query's performance using a dataset and explain its execution plan.
Scenario:
A blogging platform stores blog posts and their associated authors. Posts need to display author
details, and authors can write multiple posts.
Steps to Solve:
"postID": "p123",
"author": {
"authorID": "a123",
"email": "john@example.com"
Authors collection:
Posts collection:
Write queries to retrieve posts along with author details using joins or multiple queries.
Scenario:
A ride-sharing application stores trip data in a NoSQL database. The database needs to scale
horizontally as data grows.
Steps to Solve:
Choose a shard key (e.g., region or userID) based on query patterns.
Enable sharding in MongoDB:
sh.enableSharding("rideshareDB");
Scenario:
A company decides to migrate their relational customer database to a NoSQL database. The
relational schema has tables for customers, orders, and order items.
Steps to Solve:
"customerID": "c123",
"name": "Alice",
"orders": [
"orderID": "o123",
"date": "2025-01-01",
"items": [
}
]
1. Write a script to extract relational data and transform it into the NoSQL format.
2. Import the transformed data into the NoSQL database.
Here’s a practical work scenario that is often used in exams and assessments related to
NoSQL databases. This integrated situation includes designing, implementing, and
querying a NoSQL database to manage e-commerce data. Follow these steps to complete it:
You are tasked with creating a NoSQL database for an e-commerce application. The application
needs to manage products, customers, and orders. Each order should store the details of products
purchased, their quantities, and the total price.
Analyze the scenario to identify the key entities and their relationships:
Document-based NoSQL Database (e.g., MongoDB) is ideal for this scenario because:
o It supports nested structures, making it easy to store hierarchical data like orders
with product details.
o It scales horizontally for large e-commerce applications.
Install MongoDB on your system or use a cloud service like MongoDB Atlas.
Verify the installation using the Mongo shell or a GUI tool like MongoDB Compass.
1. Products
"_id": "p001",
"name": "Smartphone",
"price": 500,
"category": "Electronics"
2. Customers
{
"_id": "c001",
"name": "Alice",
"email": "alice@example.com",
"address": "123 Main St, Cityville"
}
3. Orders
{
"_id": "o001",
"customer_id": "c001",
"order_date": "2025-01-01",
"products": [
{"product_id": "p001", "quantity": 2, "price": 500},
{"product_id": "p002", "quantity": 1, "price": 300}
],
"total_price": 1300
}
6. Perform Queries
db.products.find();
Find orders by a specific customer:
db.orders.find({ customer_id: "c001" });
Calculate the total revenue (aggregate query):
db.orders.aggregate([
{ $group: { _id: null, total_revenue: { $sum: "$total_price" } } }
]);
Find products in a specific category:
db.products.find({ category: "Electronics" });
This practical task covers key areas such as data modeling, database interaction, and query
writing, which are essential for exams on NoSQL development. Let me know if you need further
explanation or additional tasks!
NoSQL databases are connected with various systems and tools to enable efficient storage,
retrieval, and processing of data. Here's what NoSQL databases are commonly connected with:
1. Programming Languages
NoSQL databases connect with applications written in different programming languages. These
languages interact with the database through drivers or libraries.
Examples:
o MongoDB with Python (PyMongo), Java (MongoDB Java Driver).
o Redis with Python (redis-py), Node.js (ioredis).
o Cassandra with Java (Datastax Java Driver), Python (cassandra-driver).
2. Applications
3. Cloud Platforms
NoSQL databases are connected to cloud infrastructure for scalability, backup, and easy
management.
Examples:
o AWS (Amazon DynamoDB, MongoDB Atlas).
o Google Cloud (Firestore, Bigtable).
o Microsoft Azure (Cosmos DB).
4. APIs
REST APIs: Backend servers use RESTful endpoints to interact with NoSQL databases.
GraphQL APIs: Query and manipulate data using a schema-based approach.
5. Frontend Frameworks
React, Angular, Vue.js send requests to a backend that interacts with a NoSQL database.
7. Real-Time Systems
Examples:
o Kafka for real-time data pipelines.
o Redis for caching and session management.
9. Other Databases
SQL Databases for hybrid solutions (e.g., using MongoDB with MySQL).
Data Warehouses for analytics (e.g., exporting MongoDB data to Snowflake).
APIs (Application Programming Interfaces) are a set of rules, protocols, and tools that allow
different software applications to communicate with each other. They act as an intermediary,
enabling one system to access the functionality or data of another system in a standardized and
secure way.
Types of APIs
1. Web APIs:
Allow communication between systems over the internet using protocols like
o
HTTP/HTTPS.
o Examples:
REST APIs (Representational State Transfer): Common for web
applications.
GraphQL APIs: Provide flexible queries with defined schemas.
SOAP APIs (Simple Object Access Protocol): Used in enterprise
applications.
2. Operating System APIs:
o Allow applications to interact with the operating system (e.g., Windows API,
POSIX API).
3. Library or Framework APIs:
o Provide predefined functions for specific tasks, like data processing or UI
rendering (e.g., TensorFlow API for machine learning).
4. Hardware APIs:
o Enable software to interact with hardware components like cameras, sensors, or
printers.
1. Request:
o A client (e.g., a web application) sends a request to the API with specific
parameters or data.
2. Processing:
o The API processes the request, interacting with the server, database, or underlying
application.
3. Response:
o The API sends back a response, usually in JSON or XML format.
NoSQL databases can be connected and used with many programming languages. Each
NoSQL database typically provides official drivers, libraries, or APIs for popular
programming languages to make integration seamless.
1. Python
# Insert a document
3. Java
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class Main {
collection.insertOne(product);
System.out.println("Product inserted");
4. C# (.NET)
5. PHP
<?php
require 'vendor/autoload.php';
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->ecommerce->products;
?> 6. Go
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
func main() {
if err != nil {
panic(err)
}
collection := client.Database("ecommerce").Collection("products")
7. Ruby
Popular for web applications, especially with the Ruby on Rails framework.
Libraries/Drivers:
o MongoDB: mongoid
o Redis: redis-rb
In summary, NoSQL databases connect with almost all modern programming languages through
dedicated libraries or APIs. The choice of language depends on the project requirements and
your expertise. Let me know if you need specific guidance on a language or database!
Here’s an integrated practice that involves using Python and MongoDB to manage student
course data and grades for a university system. This scenario tests your ability to model
data, interact with NoSQL databases, and query data.
You are tasked with developing a simple student course management system for a university.
The system will:
2. Database Design
Students Collection:
"_id": "s001",
"age": 20,
"email": "john.doe@example.com"
Courses Collection:
"_id": "c101",
"credits": 3
Enrollments Collection:
"student_id": "s001",
"course_id": "c101",
"grade": "A"
# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client['university']
# Create collections
students = db['students']
courses = db['courses']
enrollments = db['enrollments']
students.insert_one({
"_id": "s001",
"age": 20,
"email": "john.doe@example.com"
})
courses.insert_one({
"_id": "c101",
"credits": 3
})
enrollments.insert_one({
"student_id": "s001",
"course_id": "c101",
"grade": "A"
})
pipeline = [
"$lookup": {
"from": "courses",
"localField": "course_id",
"foreignField": "_id",
"as": "course_info"
result = enrollments.aggregate(pipeline)
print(record)
Use MongoDB’s aggregation pipeline to calculate the average grade for each course:
pipeline = [
"$group": {
"_id": "$course_id",
result = enrollments.aggregate(pipeline)
print(record)
Ensure that the data is properly inserted and that queries return the correct results.
Test different queries and ensure proper error handling.
Test edge cases (e.g., a student with no grades or courses).
Assessment Criteria:
Real-world application: It simulates the kind of data-driven applications that would use
NoSQL databases for scalability and flexibility.
Comprehensive Skills: It tests knowledge of NoSQL design, CRUD operations, and
advanced queries in MongoDB.
Connection between Programming and Database: It integrates Python, a commonly
used language in education, with MongoDB, one of the most used NoSQL databases in
the industry.