[go: up one dir, main page]

0% found this document useful (0 votes)
53 views36 pages

LU 3 and LU 4 NoSQL

The document provides a comprehensive guide on implementing database design using MongoDB, covering data definition, manipulation, query optimization, user management, security, and deployment. It details the use of JSON-like documents, CRUD operations, role-based access control, and best practices for securing and managing databases. Additionally, it includes practical exercises for creating and managing a NoSQL database for various applications, such as e-commerce and library management systems.

Uploaded by

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

LU 3 and LU 4 NoSQL

The document provides a comprehensive guide on implementing database design using MongoDB, covering data definition, manipulation, query optimization, user management, security, and deployment. It details the use of JSON-like documents, CRUD operations, role-based access control, and best practices for securing and managing databases. Additionally, it includes practical exercises for creating and managing a NoSQL database for various applications, such as e-commerce and library management systems.

Uploaded by

hiti protogene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

LU 3: Implement Database Design L5 SOD

3.1 MongoDB Data Definition

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:

db.createCollection("collectionName", { capped: true, size: 10000 })

 Indexes: Optimize data retrieval with indexes


Create indexes to improve query performance (e.g., Single Field, Compound, Text Indexes).
Syntax:

db.collection.createIndex({ field: 1 }) // 1 for ascending, -1 for descending

Schema Validation:

 Use validation rules to enforce data consistency.


 Example:

db.createCollection("example", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["field1", "field2"],
properties: {
field1: { bsonType: "string" },
field2: { bsonType: "number" }
}
}
}
});

3.2 MongoDB Data Manipulation Based on Database Requirements

CRUD Operations:

Create:

db.collection.insertOne({ key: "value" });

db.collection.insertMany([{ key: "value" }, { key: "value2" }]);

Read:

db.collection.find({ key: "value" });

Update:

db.collection.updateOne({ key: "value" }, { $set: { newKey: "newValue" } });

Delete:

db.collection.deleteOne({ key: "value" });

Bulk Operations: Perform multiple operations in one call for efficiency:

db.collection.bulkWrite([

{ insertOne: { document: { key: "value" } } },

{ updateOne: { filter: { key: "value" }, update: { $set: { key: "newValue" } } } }

]);
 Insert Data:
o Insert one or multiple documents into a collection.

db.collection.insertOne({ field1: "value1", field2: "value2" })


db.collection.insertMany([{ field1: "value1" }, { field1: "value2" }])

 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.

db.collection.updateOne({ field1: "value1" }, { $set: { field2: "newValue" } })

Delete Data:

 Remove specific documents or all documents in a collection.

db.collection.deleteOne({ field1: "value1" })


db.collection.deleteMany({ field1: { $exists: true } })

3.3 Query Optimization

 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 } } }
])

 Avoid Full Collection Scans:

 Use specific queries to reduce scanning unnecessary documents.

 Monitor Query Performance


Use explain() to analyze query performance.
db.collection.find({ field1: "value1" }).explain("executionStats")
LU 4: Manage Database

4.1 Database User Management

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" }]
})

Authentication: Enable user authentication to secure database access.

 Update Roles:
o Modify user roles and permissions when necessary.
 Audit User Actions:
o Track user activities for better accountability.

4.2 Database Security

 Authentication:

 Use MongoDB’s built-in authentication mechanisms.

 Encryption:

 Enable data-at-rest and in-transit encryption.

At Rest: Use storage encryption for sensitive data.

In Transit: Use TLS/SSL to secure data transmission.


 TLS/SSL (Transport Layer Security / Secure Sockets Layer)

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).

 Role-Based Access Control (RBAC):

 Assign roles like read, readWrite, and dbAdmin based on the principle of least privilege.

 Network Security:

 Use firewalls and VPNs to restrict access.

 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.

 Backup and Restore:


o Regularly back up databases using mongodump and restore with mongorestore.

4.3 Database Deployment

 Select Deployment Type:

Decide on standalone, replica set, or sharded cluster based on application needs.

 Standalone: Suitable for development and testing.


 Replica Set: For high availability and fault tolerance.
 Sharded Cluster: For horizontal scaling and handling large datasets.

 Optimize for Target Environment:


o Choose appropriate hardware, storage, and configurations (e.g., journaling,
memory allocation).
 Deployment Steps:

 Install MongoDB on servers.


 Configure the mongod.conf file for environment-specific settings.
 Start MongoDB and verify configurations.
mongod --config /etc/mongod.conf

Key Considerations for L5 SOD

 Scalability: Design for scalability, especially for high-traffic applications.


 Monitoring: Use tools like MongoDB Atlas, Cloud Manager, or open-source monitoring
solutions.
 Testing: Regularly test the database for performance bottlenecks and security
vulnerabilities.
 Documentation: Maintain documentation for schema design, queries, and security
measures.

Best Practice Exercises for MongoDB and NoSQL Database Topics

LU 3: Implement Database Design

Exercise 1: Data Definition in MongoDB


Question:
You are designing a database for an e-commerce platform that manages product inventory.
Define a MongoDB collection structure for the Products collection, including fields for
product_id, name, category, price, quantity, and created_at. Use best practices for
indexing to enhance query performance.

Answer:

// Data definition for Products collection


db.createCollection("Products", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["product_id", "name", "price", "quantity", "created_at"],
properties: {
product_id: {
bsonType: "string",
description: "Unique identifier for the product"
},
name: {
bsonType: "string",
description: "Product name"
},
category: {
bsonType: "string",
description: "Category of the product"
},
price: {
bsonType: "double",
description: "Price of the product"
},
quantity: {
bsonType: "int",
description: "Stock quantity"
},
created_at: {
bsonType: "date",
description: "Date when the product was added"
}
}
}
}
});

// Index for optimizing queries by category and price


db.Products.createIndex({ category: 1, price: -1 });

Exercise 2: Data Manipulation in MongoDB


Question:
Add a new product to the Products collection. Then, update the price of all products in the
Electronics category by reducing it by 10%. Finally, delete all products with a quantity of 0.

Answer:

// Insert a new product


db.Products.insertOne({
product_id: "P001",
name: "Wireless Headphones",
category: "Electronics",
price: 120.0,
quantity: 50,
created_at: new Date()
});

// Update prices of all products in the Electronics category


db.Products.updateMany(
{ category: "Electronics" },
{ $mul: { price: 0.9 } } // Reduce price by 10%
);

// Delete products with quantity 0


db.Products.deleteMany({ quantity: 0 });
Exercise 3: Query Optimization
Question:
You are tasked with optimizing a query that retrieves products in the Furniture category priced
above $500. Explain the steps to optimize the query.

Answer:

1. Create an Index:

db.Products.createIndex({ category: 1, price: 1 });


2. Execute the Query:
db.Products.find({ category: "Furniture", price: { $gt: 500 } });

3. Analyze Performance: Use the explain() method to evaluate query performance.

db.Products.find({ category: "Furniture", price: { $gt: 500 } }).explain("executionStats");

4. Adjust Indexes: If necessary, create a compound index to further optimize for additional fields used in
queries.

LU 4: Manage Database

Exercise 1: Managing Users and Permissions


Question:
Create a new user with read and write permissions on the ecommerce database and restrict them
from accessing administrative operations.

Answer:

db.createUser({

user: "ecom_user",

pwd: "securepassword123",

roles: [

{ role: "readWrite", db: "ecommerce" }

});

Exercise 2: Securing the Database


Question:
List three best practices to secure a MongoDB database and demonstrate how to implement one
of them.

Answer:
Best Practices:

1. Enable authentication.
2. Use TLS/SSL for secure connections.
3. Restrict network access using IP whitelisting.

Implementation Example: Enable Authentication

1. Modify the mongod.conf file to include:

security:

authorization: "enabled"

2. Restart the MongoDB service:

sudo systemctl restart mongod

Exercise 3: Deploying the Database


Question:
Deploy a MongoDB database to a production environment and explain how to ensure it is
properly monitored and backed up.

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:

 Use tools like MongoDB Atlas Monitoring or Prometheus with Grafana.

3. Backup:

 Use mongodump for periodic backups.

mongodump --db ecommerce --out /backups/$(date +%F)


Practice Exercise:
Practice Work1: Build a NoSQL Database for a Library Management System

Objective:

Develop a NoSQL database to manage a library's book inventory, user details, and borrowing
records using a document-based database like MongoDB.

Step 1: Define the Requirements

 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:

o Add new books to the library.


o Add new users.
o Record book borrowing and returning.
o Query available books by genre, author, or title.
o Track books currently borrowed by a user.

Step 2: Plan the Database Schema

Design collections for:

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"
}

Step 3: Setup the Database

 Install MongoDB: Use MongoDB Community Edition or MongoDB Atlas (cloud-


based).
 Connect to MongoDB: Use a programming language like Python or Node.js with
MongoDB drivers.

Step 4: Write Queries

Basic Queries

1. Insert books and users:


o Use insertOne or insertMany.
2. Query all books available in a specific genre.
3. Find books borrowed by a specific user.

Advanced Queries

1. Update a book’s availability status when borrowed or returned.


2. Aggregate data:
o Count the total number of books by genre.
o List the top 5 most borrowed books.

Step 5: Build and Test CRUD Operations

Develop scripts or an application to:

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).

Step 6: (Optional) Create a Simple Application

Create a basic frontend or command-line interface that:

1. Accepts user inputs (e.g., search for books or add users).


2. Connects to your NoSQL database and performs operations.

Deliverables:

1. A well-documented script for database creation and interaction.


2. Sample data for books, users, and transactions.
3. A report on your experience, including challenges faced and how you resolved them.

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

 Download and install MongoDB: MongoDB Download Center.


 Install MongoDB Compass (optional GUI for MongoDB) to visualize and manage your data.

2. Design the Database Schema

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,

"genres": ["Sci-Fi", "Thriller"],

"director": "Christopher Nolan",

"actors": ["Leonardo DiCaprio", "Joseph Gordon-Levitt", "Ellen Page"],

"ratings": [

{"user": "user1", "rating": 9.5, "review": "Amazing movie!"},

{"user": "user2", "rating": 8.5, "review": "Great visuals and story!"}

}
3. Set Up the Environment

 Start the MongoDB server by running the MongoDB daemon (mongod).


 Connect to MongoDB using the MongoDB shell (mongosh) or Compass.

4. Insert Sample Data

 Open the MongoDB shell or Compass.


 Create a new database called movie_catalog:

use movie_catalog

Insert sample movies:

db.movies.insertMany([

"title": "Inception",

"release_year": 2010,

"genres": ["Sci-Fi", "Thriller"],

"director": "Christopher Nolan",

"actors": ["Leonardo DiCaprio", "Joseph Gordon-Levitt", "Ellen Page"],

"ratings": [

{"user": "user1", "rating": 9.5, "review": "Amazing movie!"},

{"user": "user2", "rating": 8.5, "review": "Great visuals and story!"}

},

"title": "The Dark Knight",

"release_year": 2008,
"genres": ["Action", "Crime", "Drama"],

"director": "Christopher Nolan",

"actors": ["Christian Bale", "Heath Ledger", "Aaron Eckhart"],

"ratings": [

{"user": "user3", "rating": 10, "review": "Masterpiece!"},

{"user": "user4", "rating": 9, "review": "Outstanding performance!"}

])

5. Query the Database

Practice querying the database:

 Find all movies:

db.movies.find()

Find movies directed by Christopher Nolan:

db.movies.find({"director": "Christopher Nolan"})

Find movies released after 2009:

db.movies.find({"release_year": {$gt: 2009}})

Find movies with a rating above 9:

db.movies.find({"ratings.rating": {$gt: 9}})

6. Update Data

 Add a new genre to a movie:

db.movies.updateOne(

{"title": "Inception"},
{$addToSet: {"genres": "Mystery"}}

7. Delete Data

 Delete a movie by title:

db.movies.deleteOne({"title": "The Dark Knight"})

8. Build a Simple Application (Optional)

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:

 Add new movies.


 View movie details.
 Submit reviews and ratings.

Outcome

By the end of this exercise, you’ll have:

 Designed and implemented a NoSQL schema.


 Learned how to insert, query, update, and delete data.
 Built familiarity with MongoDB and NoSQL concepts.

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.

1. Design a Schema for a Document-Oriented NoSQL Database

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:

 Choose a NoSQL database (e.g., MongoDB).


 Design the schema to store products as documents.
o Example schema:

{
"productID": "12345",

"name": "Laptop",

"price": 1200,

"category": "Electronics",

"description": "High-performance laptop",

"reviews": [

{ "userID": "u1", "rating": 5, "comment": "Excellent!" },

{ "userID": "u2", "rating": 4, "comment": "Good value." }

Use MongoDB queries to insert, update, delete, and fetch product data.

2. Optimize a NoSQL Query for Performance

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:

Create an index on the userID and timestamp fields.

db.posts.createIndex({ userID: 1, timestamp: -1 });

Write a query to fetch posts:

db.posts.find({ userID: "user123" }).sort({ timestamp: -1 });

Test the query's performance using a dataset and explain its execution plan.

3. Handle Relationships in a NoSQL Database

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:

 Use an embedded or reference-based model:


o Embedded (author data stored within each post):

"postID": "p123",

"title": "NoSQL Basics",

"content": "Introduction to NoSQL...",

"author": {

"authorID": "a123",

"name": "John Doe",

"email": "john@example.com"

Reference (author data stored separately, referenced by post):

 Authors collection:

{ "authorID": "a123", "name": "John Doe", "email": "john@example.com" }

Posts collection:

{ "postID": "p123", "title": "NoSQL Basics", "content": "Introduction to NoSQL...", "authorID":


"a123" }

Write queries to retrieve posts along with author details using joins or multiple queries.

4. Implement a Sharding Strategy

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");

sh.shardCollection("rideshareDB.trips", { region: 1 });

 Insert data and verify distribution across shards.


 Test query performance and ensure balanced workload.

5. Migrate from a Relational to NoSQL Database

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:

1. Analyze the existing schema and identify relationships.


2. Design a NoSQL schema (e.g., nested documents for orders and items):

"customerID": "c123",

"name": "Alice",

"orders": [

"orderID": "o123",

"date": "2025-01-01",

"items": [

{ "productID": "p1", "quantity": 2 },

{ "productID": "p2", "quantity": 1 }

}
]

1. Write a script to extract relational data and transform it into the NoSQL format.
2. Import the transformed data into the NoSQL database.

Study and Practice Tips

 Hands-on Practice: Use tools like MongoDB Atlas, DynamoDB, or Couchbase to


practice these tasks.
 Focus on Query Writing: Be comfortable writing queries for CRUD operations,
indexing, aggregation, and joins.
 Analyze Use Cases: Understand which NoSQL model (document, key-value, columnar,
graph) suits specific scenarios.
 Read Documentation: Refer to official NoSQL database documentation for advanced
features like sharding, indexing, and replication.
 Practice Exam Questions: Solve previous exam questions or mock problems to
reinforce concepts.

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:

Scenario: E-Commerce Data Management with NoSQL

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.

Steps to Complete the Task

1. Understand the Requirements

Analyze the scenario to identify the key entities and their relationships:

 Entities: Products, Customers, Orders.


 Relationships:
o Orders belong to a Customer.
o Orders contain multiple Products.

2. Choose the Appropriate NoSQL Database Type

 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.

3. Install and Set Up the NoSQL Database

 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.

4. Design the Database Structure

Create collections for:

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
}

5. Insert Sample Data

Use the following commands in MongoDB to insert sample documents:

// Insert into Products


db.products.insertMany([
{ _id: "p001", name: "Smartphone", price: 500, category: "Electronics" },
{ _id: "p002", name: "Laptop", price: 300, category: "Electronics" }
]);

// Insert into Customers


db.customers.insertOne({
_id: "c001",
name: "Alice",
email: "alice@example.com",
address: "123 Main St, Cityville"
});

// Insert into Orders


db.orders.insertOne({
_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

Here are some example queries you might be asked to perform:


 Retrieve all products:

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" });

7. Test and Validate

 Ensure your data model supports the required queries efficiently.


 Test your queries for accuracy and performance.

8. Document Your Work

 Describe the structure of your collections.


 Provide examples of the data you inserted.
 Include the queries and their results.

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

NoSQL databases are used in:

 Web Applications: Storing dynamic content like user profiles or posts.


 Mobile Applications: Managing offline and real-time data (e.g., Firebase Realtime
Database).
 IoT Devices: For processing and storing sensor data in real-time.

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

Applications connect to NoSQL databases through 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

Frontend frameworks connect indirectly to NoSQL databases via APIs:

 React, Angular, Vue.js send requests to a backend that interacts with a NoSQL database.

6. Analytics and Data Visualization Tools

NoSQL databases are connected with:


 Data Analysis: Apache Spark, Hadoop (e.g., Cassandra integration).
 Visualization: Tableau, Power BI, Kibana (for Elasticsearch).

7. Real-Time Systems

NoSQL databases are connected to systems for real-time processing:

 Examples:
o Kafka for real-time data pipelines.
o Redis for caching and session management.

8. Authentication and Authorization Services

NoSQL databases often connect with security tools:

 OAuth for token-based authentication.


 LDAP or Active Directory for user management.

9. Other Databases

NoSQL databases sometimes connect with:

 SQL Databases for hybrid solutions (e.g., using MongoDB with MySQL).
 Data Warehouses for analytics (e.g., exporting MongoDB data to Snowflake).

In summary, NoSQL databases connect with programming languages, applications, cloud


platforms, APIs, analytics tools, real-time systems, and security/authentication services to
serve diverse business needs.

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.

How APIs Work

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.

Programming Languages Commonly Used with NoSQL Databases

1. Python

 Widely used for data science, backend development, and automation.


 Libraries/Drivers:
o MongoDB: PyMongo, MongoEngine
o Cassandra: Cassandra-driver
o Redis: redis-py

Example (MongoDB with PyMongo):

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/ecommerce', { useNewUrlParser: true });

const Product = mongoose.model('Product', { name: String, price: Number });

const newProduct = new Product({ name: 'Smartphone', price: 500 });

newProduct.save().then(() => console.log('Product saved')); collection = db["products"]

# Insert a document

collection.insert_one({"name": "Laptop", "price": 800})

3. Java

 Popular for enterprise-level applications.


 Libraries/Drivers:
o MongoDB: MongoDB Java Driver
o Cassandra: DataStax Java Driver
o Redis: Jedis

Example (MongoDB with Java):

import com.mongodb.client.MongoClients;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;

import org.bson.Document;
public class Main {

public static void main(String[] args) {

var client = MongoClients.create("mongodb://localhost:27017");

MongoDatabase database = client.getDatabase("ecommerce");

MongoCollection<Document> collection = database.getCollection("products");

Document product = new Document("name", "Tablet").append("price", 300);

collection.insertOne(product);

System.out.println("Product inserted");

4. C# (.NET)

 Used for enterprise applications and desktop software.


 Libraries/Drivers:
o MongoDB: MongoDB .NET Driver
o Redis: StackExchange.Redis

Example (MongoDB with C#):

5. PHP

 Common for web applications.


 Libraries/Drivers:
o MongoDB: MongoDB PHP Driver
o Redis: PhpRedis

Example (MongoDB with PHP):

<?php

require 'vendor/autoload.php';
$client = new MongoDB\Client("mongodb://localhost:27017");

$collection = $client->ecommerce->products;

$insertResult = $collection->insertOne(['name' => 'Headphones', 'price' => 100]);

echo "Inserted with Object ID '{$insertResult->getInsertedId()}'";

?> 6. Go

 Favored for high-performance backend services.


 Libraries/Drivers:
o MongoDB: go.mongodb.org/mongo-driver
o Redis: go-redis

Example (MongoDB with Go):

package main

import (

"context"

"fmt"

"go.mongodb.org/mongo-driver/mongo"

"go.mongodb.org/mongo-driver/mongo/options"

func main() {

client, err := mongo.Connect(context.TODO(),


options.Client().ApplyURI("mongodb://localhost:27017"))

if err != nil {

panic(err)
}

collection := client.Database("ecommerce").Collection("products")

product := map[string]interface{}{"name": "Keyboard", "price": 50}

result, _ := collection.InsertOne(context.TODO(), product)

fmt.Println("Inserted ID:", result.InsertedID)

7. Ruby

 Popular for web applications, especially with the Ruby on Rails framework.
 Libraries/Drivers:
o MongoDB: mongoid
o Redis: redis-rb

8. Other Supported Languages

 C++: MongoDB C++ Driver, hiredis for Redis.


 Scala: Akka Persistence with Cassandra, Scala MongoDB Driver.
 Kotlin: Works with Java drivers for MongoDB, Redis, and Cassandra.
 R: For data analysis, integrates with MongoDB (mongolite package).

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!

N.B:WORK ON THESE INTEGRATED SITUATION


Integrated Practice Scenario Using Python and MongoDB

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.

Scenario: University Student Course Management System

You are tasked with developing a simple student course management system for a university.
The system will:

 Track students and their personal details.


 Store courses that students are enrolled in.
 Track student grades for each course.

Step-by-Step Process to Solve the Scenario

1. Set Up MongoDB and Python Environment

 Install MongoDB locally or use a cloud service like MongoDB Atlas.


 Install Python and the PyMongo library to interact with MongoDB:

pip install pymongo

2. Database Design

You will create three collections:

 Students: Stores student information.


 Courses: Stores course details.
 Enrollments: Stores which student is enrolled in which course and their grade.

Example Document Structure:

 Students Collection:

"_id": "s001",

"name": "John Doe",

"age": 20,
"email": "john.doe@example.com"

Courses Collection:

"_id": "c101",

"course_name": "Data Structures",

"credits": 3

Enrollments Collection:

"student_id": "s001",

"course_id": "c101",

"grade": "A"

3. Inserting Data Using Python

Use the following Python code to insert data into MongoDB.

from pymongo import MongoClient

# Connect to MongoDB

client = MongoClient("mongodb://localhost:27017/")

db = client['university']

# Create collections
students = db['students']

courses = db['courses']

enrollments = db['enrollments']

# Insert Student Data

students.insert_one({

"_id": "s001",

"name": "John Doe",

"age": 20,

"email": "john.doe@example.com"

})

# Insert Course Data

courses.insert_one({

"_id": "c101",

"course_name": "Data Structures",

"credits": 3

})

# Insert Enrollment Data

enrollments.insert_one({

"student_id": "s001",

"course_id": "c101",
"grade": "A"

})

4. Querying the Database

You need to perform the following tasks:

 Query to find all students and their courses:

# Find all students and their courses with grades

pipeline = [

"$lookup": {

"from": "courses",

"localField": "course_id",

"foreignField": "_id",

"as": "course_info"

result = enrollments.aggregate(pipeline)

for record in result:

print(record)

Query to get a student's details and their grades:

student_data = students.find_one({"_id": "s001"})

enrollment_data = enrollments.find({"student_id": "s001"})

for enrollment in enrollment_data:


course = courses.find_one({"_id": enrollment['course_id']})

print(f"Student: {student_data['name']}, Course: {course['course_name']}, Grade:


{enrollment['grade']}")

5. Aggregate Function to Calculate Average Grades for Each Course

Use MongoDB’s aggregation pipeline to calculate the average grade for each course:

# Calculate average grade per course

pipeline = [

"$group": {

"_id": "$course_id",

"average_grade": {"$avg": "$grade"}

result = enrollments.aggregate(pipeline)

for record in result:

print(record)

6. Test and Validate

 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:

 Data Modeling: Proper design of collections (students, courses, and enrollments).


 Python Integration: Correctly using Python with MongoDB to insert and query data.
 Complex Queries: Using aggregation and joins ($lookup in MongoDB) to retrieve and
manipulate data.
 Problem-Solving: Ability to solve queries using MongoDB's NoSQL approach.

Why This Practice is Relevant for Exams:

 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.

You might also like