NoSQL Lec
NoSQL Lec
Overview
• http://martinfowler.com/articles/nosqlKeyPoints.html
Unlike traditional relational databases
(RDBMS) which use structured query
language (SQL) and are table-based, NoSQL
databases can store and retrieve
unstructured or semi-structured data.
Why is NoSQL needed?
• Relational databases have been a successful technology
for twenty years, providing persistence, concurrency
control, and an integration mechanism
• Key-value
• Document
• Column-family
• Graph
Relational database: order example
Key-Value Stores:
Schema:
Key: session_id
Value: JSON object with session data
2. Caching
Example: Frequently Accessed Data
Schema:
Key: query_result_key
Value: Cached query result
3. User Profiles
Example: User Information Storage
Schema:
Key: user_id
Value: JSON object with user profile data
Document
Schema:
Collection: articles
Document Fields: title, author, content, tags, published_date
{
"_id": "article123",
"title": "How to Use MongoDB for CMS",
"author": "John Doe",
"content": "This article explains how to use MongoDB for content management
systems...",
"tags": ["MongoDB", "CMS", "NoSQL"],
"published_date": "2024-06-03T12:00:00Z"
}
{
"_id": "article456",
"title": "Best Practices for NoSQL Databases",
"author": "Jane Smith",
"content": "In this article, we discuss the best practices for using NoSQL databases...",
"tags": ["NoSQL", "Database", "Best Practices"],
"published_date": "2024-06-02T11:00:00Z"
}
2. Catalogs
Example: Product Catalog
A document-based NoSQL database can be used to store product catalogs, where each
document represents a product with various attributes.
Schema:
Collection: products
Document Fields: name, description, price, category, stock
{
"_id": "product123",
"name": "Laptop",
"description": "High-performance laptop with 16GB RAM and 512GB SSD.",
"price": 999.99,
"category": "Electronics",
"stock": 50
}
{
"_id": "product456",
"name": "Smartphone",
"description": "Latest model smartphone with advanced features.",
"price": 699.99,
"category": "Electronics",
"stock": 150
}
3. User-Generated Content
Example: Blog Posts
Schema:
Collection: blog_posts
Document Fields: title, author, content, comments, posted_date
Sample data
{
"_id": "post123",
"title": "My First Blog Post",
"author": "user123",
"content": "This is the content of my first blog post...",
"comments": [
{"user": "user456", "comment": "Great post!", "date": "2024-06-03T13:00:00Z"},
{"user": "user789", "comment": "Thanks for sharing!", "date": "2024-06-03T14:00:00Z"}
],
"posted_date": "2024-06-03T12:00:00Z"
}
Column-family
Schema:
Schema:
Schema:
• Graph databases
organize data into node
and edge graphs; they
work best for data that
has complex relationship
structures
• Examples: Neo4j,
ArangoDB, Amazon
Neptune.
Graph
• Graph databases allow you to store entities
and relationships between these entities.
• Entities are also known as nodes, which have
properties. Think of a node as an instance of an object
in the application.
• Relations are known as edges that can also have
properties. Nodes are organised by relationships which
allow you to find interesting patterns between the nodes.
• The organisation of the graph lets the data be stored
just once and then interpreted in different ways based
on relationships.
Graph
Schema:
Nodes: User
Edges: FRIENDS_WITH
2. Recommendation Engines
Example: Product Recommendations
Schema: