0% found this document useful (0 votes)
7 views26 pages

NOSQL

The document discusses various database models, focusing on MongoDB's schemaless property, the disadvantages of RDBMS, and the concepts of column-oriented and document-oriented databases. It highlights the flexibility and scalability of NoSQL databases compared to traditional relational databases, along with examples of popular vendors and their offerings. Additionally, it outlines different data distribution models and the advantages of NoSQL, emphasizing their suitability for modern applications.

Uploaded by

Gowtham Gowthu
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)
7 views26 pages

NOSQL

The document discusses various database models, focusing on MongoDB's schemaless property, the disadvantages of RDBMS, and the concepts of column-oriented and document-oriented databases. It highlights the flexibility and scalability of NoSQL databases compared to traditional relational databases, along with examples of popular vendors and their offerings. Additionally, it outlines different data distribution models and the advantages of NoSQL, emphasizing their suitability for modern applications.

Uploaded by

Gowtham Gowthu
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
You are on page 1/ 26

Demonstrate Schema lessness property of MongoDB with an example.

Demonstra ng Schema lessness in MongoDB

MongoDB is a NoSQL database that follows a document-based model and supports a schema less structure.
This means:

 Collec ons do not require a fixed schema.

 Documents in the same collec on can have different fields, data types, and structures.

Example: Demonstra ng Schemaless Nature

Let’s create a students collec on and insert mul ple documents with different structures.

Step 1: Insert Documents with Different Schemas

// Document 1: Basic student info db.students.insertOne({ name: "Alice", age: 21, course: "Computer Science"
});

// Document 2: Different structure (addi onal fields) db.students.insertOne({ name: "Bob", email:
"bob@example.com", skills: ["Java", "Python"], isAc ve: true });

// Document 3: Nested structure db.students.insertOne({ name: "Charlie", address: { city: "New York", zip:
"10001" }, age: 22 });

Step 2: Retrieve All Documents

db.students.find().pre y();

Output:

{ "_id": ObjectId("..."), "name": "Alice", "age": 21, "course": "Computer Science" } { "_id": ObjectId("..."),
"name": "Bob", "email": "bob@example.com", "skills": ["Java", "Python"], "isAc ve": true } { "_id":
ObjectId("..."), "name": "Charlie", "address": { "city": "New York", "zip": "10001" }, "age": 22 }

Conclusion

This example shows that:

 MongoDB allows inser ng documents with different field names, data types, and nested objects.

 There's no need to define a rigid schema up front like in tradi onal SQL databases.

This flexibility is what we call the schemalessness property of MongoDB.

Explain disadvantages of RDBMS

Key Disadvantages of RDBMS

1. Rigid Schema (Lack of Flexibility)


 Schema must be defined before inser ng data.

 Changes in structure (like adding a new column) require schema altera on, which can be complex and
me-consuming.

2. Not Ideal for Unstructured or Semi-structured Data

 RDBMSs work best with structured data (rows and columns).

 Handling images, videos, JSON, XML, or nested data is not efficient.

3. Scalability Limita ons

 Primarily support ver cal scaling (adding more power to a single server).

 Horizontal scaling (adding more servers) is complex and costly.

4. Complex Joins Can Be Costly

 Performing mul ple joins across large tables can be slow and resource-intensive.

 Queries may become complex and harder to maintain.

5. Limited Performance with Large Volumes of Data

 Not op mized for big data or real- me analy cs.

 Performance degrades as data size increases drama cally.

6. Higher Cost for Licensing and Maintenance

 Enterprise-grade RDBMSs like Oracle or SQL Server can be expensive.

 Requires DBAs (Database Administrators) for setup, tuning, and maintenance.

7. Not Suitable for Agile/Itera ve Development

 Frequent schema changes can slow down development.

 NoSQL databases are o en preferred in agile environments due to their flexibility.

8. Poor Support for Distributed Systems

 RDBMSs are not inherently designed for distributed architectures.

 Implemen ng replica on, sharding, and high availability requires addi onal setup and complexity.

Explain the column-oriented database concepts and relevant vendors packages.

What is a Column-Oriented Database?

A column-oriented database stores data by columns rather than by rows. This means that data from the same
column across mul ple rows is stored together.
Comparison with Row-Oriented Databases:

 Row-oriented: stores en re rows together (e.g., MySQL, PostgreSQL).

 Column-oriented: stores values from each column in separate files or structures.

Key Concepts of Column-Oriented Databases

Feature Descrip on

Data Storage Columns stored together instead of rows.

Compression High compression rates due to similar data types in columns.

Performance Faster for analy cal queries like SUM, AVG, COUNT on large datasets.

OLAP Focus Suited for read-heavy workloads and analy cal processing (OLAP).

Schema Flexibility Some column stores also support dynamic schemas.

Use Cases

 Data Warehousing

 Business Intelligence

 Real- me Analy cs

 Time-Series Data Analysis

Popular Column-Oriented Database Vendors & Packages

Vendor Database Descrip on

Amazon Amazon Cloud-based data warehouse built on PostgreSQL and op mized for OLAP
Redshi with columnar storage.

Google BigQuery Fully-managed serverless data warehouse with columnar storage and
SQL-like querying.

ClickHouse, ClickHouse Open-source, high-performance columnar DB for real- me analy cs.


Inc. Excellent for me-series and dashboards.

Apache Apache Columnar file format used by big data tools like Hive, Spark. Not a DB
Parquet itself, but a core part of columnar processing.

Ver ca Ver ca Commercial column-store DB op mized for big data analy cs with
extremely fast performance.

Apache Apache Kudu Hybrid between HDFS and tradi onal column stores, op mized for fast
analy cs.
SAP SAP HANA In-memory, columnar DB for real- me data processing and analy cs.

InfluxData InfluxDB Purpose-built me-series database using columnar concepts for high-
speed inges on and queries.

Example: How Columnar Storage Works

Suppose we have a table:

ID Name Age

1 John 25

2 Jane 28

3 Max 30

 Row Store: stores as [(1, John, 25), (2, Jane, 28), (3, Max, 30)]

 Column Store: stores as [1, 2, 3], [John, Jane, Max], [25, 28, 30]

This layout is faster for queries like:

SELECT AVG(Age) FROM students;

Summary

Pros Cons

Fast analy cal queries Slower for OLTP (frequent writes/updates)

Excellent compression Complex to manage transac onal opera ons

Op mized for big data Requires specialized knowledge

Discuss document-oriented database concepts and relevant vendors' packages in detail.

What is a Document-Oriented Database?

A document-oriented database is a type of NoSQL database designed to store, retrieve, and manage semi-
structured data as documents, typically in JSON, BSON, or XML formats.

Key Concepts of Document-Oriented Databases

Feature Descrip on

Document A self-contained data unit resembling a JSON object (key-value pairs, arrays,
nested structures).
Collec on A group of related documents (similar to a table in RDBMS but without a fixed
schema).

Schemaless Documents in the same collec on can have different structures and fields.

Indexing Documents can be indexed on any field for faster querying.

Embedded/Nested Supports nested objects and arrays for modeling complex rela onships within a
Data single document.

Example Document (JSON format)

{ "_id": 1, "name": "Alice", "email": "alice@example.com", "skills": ["Java", "Python"], "address": { "city":
"Bangalore", "zip": "560001" } }

 Each document is self-descrip ve and can vary from others in structure.

 Data can be queried using a rich query language similar to JSON syntax.

Benefits of Document Databases

Advantage Descrip on

Flexible Schema Easily modify structure without altering schema (ideal for agile development).

Natural Data Mapping Ideal for applica ons using JSON (e.g., JavaScript, web apps).

High Performance Excellent for read and write-heavy opera ons.

Scalability Easily scaled horizontally using sharding.

Aggrega on Support Built-in pipelines to transform and analyze data efficiently.

Popular Document-Oriented Database Vendors & Packages

Vendor Database Descrip on

MongoDB MongoDB Most widely-used open-source document DB, stores data in BSON,
Inc. supports rich queries, indexing, and aggrega on.

Amazon Amazon Managed MongoDB-compa ble document database, op mized for


AWS DocumentDB scalability and AWS ecosystem.

Couchbase Couchbase Enterprise NoSQL database with JSON storage, in-memory caching, SQL-
Inc. like querying (N1QL), and mobile sync.

Apache CouchDB Open-source document store using JSON documents, HTTP REST API,
and MVCC for versioning and conflict resolu on.
Microso Azure Cosmos DB Globally distributed NoSQL database suppor ng mul ple models,
including document model with MongoDB API support.

MarkLogic MarkLogic Server Mul -model DB with strong enterprise features, including document,
seman c, and search capabili es.

RavenDB RavenDB Open-source document database with ACID guarantees and built-in full-
text search and replica on.

OrientDB OrientDB A mul -model DB that supports document, graph, object, and key/value
models.

Comparison: MongoDB vs CouchDB

Feature MongoDB CouchDB

Storage Format BSON JSON

Query Language MongoDB Query Language RESTful HTTP API

ACID Compliance At document-level At database-level

Use Case Scalable, high-performance apps Offline-first, distributed apps

Challenges with Document-Oriented Databases

Challenge Descrip on

Complex Rela onships Not ideal for deeply rela onal data (joins are limited or inefficient).

Consistency Eventual consistency in distributed setups may not suit all applica ons.

Overhead Large or highly nested documents may cause performance issues.

Indexing Improper indexing can lead to slow queries, especially with dynamic schemas.

Use Cases

 Content management systems

 User profile storage

 Product catalogs

 Real- me analy cs

 IoT & mobile apps


 E-commerce pla orms

Summary

Feature Descrip on

Type NoSQL – Document-Oriented

Data Format JSON, BSON, XML

Best For Semi-structured, nested, and flexible data

Key Benefit Schema flexibility and scalability

Key Vendors MongoDB, CouchDB, Couchbase, DocumentDB, Cosmos DB

Explain various flavors of data distribu on models with NEAT diagram.

Data Distribu on Models

Data distribu on refers to how data is stored, accessed, and synchronized across mul ple nodes or loca ons
in a distributed system.

1. Centralized Model

 Descrip on: All data is stored in a single loca on.

 Pros: Easy to manage, consistent data.

 Cons: Single point of failure, not scalable.

2. Replicated Model

 Descrip on: Copies of the same data are stored on mul ple nodes.

 Types:

 Master-Slave Replica on: One node is the master, others are read-only slaves.

 Mul -Master Replica on: All nodes can read/write, requiring conflict resolu on.

 Pros: High availability and read performance.

 Cons: Write conflicts, increased storage and sync overhead.

3. Par oned Model (Sharding)

 Descrip on: Dataset is split into parts (shards) and distributed across nodes.

 Pros: Scales horizontally, supports large datasets.

 Cons: Querying across shards can be complex, risk of data skew.


4. Hybrid Model

 Descrip on: Combines replica on and par oning (e.g., each shard is replicated).

 Pros: Balanced scalability and availability.

 Cons: Complex setup and maintenance.

Summary Table

Model Scalable High Availability Complex Setup Use Case

Centralized Small applica ons

Replicated (Reads) Moderate Read-heavy apps, backups

Par oned (alone) Moderate Big data, analy cs

Hybrid (Complex) Enterprise-grade, large-scale systems

Advantages of NoSQL

Advantages of NoSQL Databases

NoSQL databases offer several benefits over tradi onal rela onal databases, especially for modern
applica ons that demand scalability, flexibility, and performance.

1. Schema Flexibility (Schemaless)

 No fixed schema—documents or records can have different fields.

 Ideal for agile development and frequent data model changes.

2. Horizontal Scalability

 Easily scaled across mul ple servers using sharding.

 Supports large-scale web apps, real- me analy cs, and IoT data.

3. High Performance

 Op mized for fast reads and writes.

 Suitable for high-throughput applica ons like messaging systems, recommenda on engines, etc.

4. Supports Unstructured & Semi-structured Data

 Handles JSON, XML, key-value pairs, graphs, and more.

 Ideal for storing logs, social media data, sensor data, and product catalogs.

5. Variety of Data Models


 Supports mul ple models:

 Document-based (e.g., MongoDB)

 Key-value (e.g., Redis)

 Column-family (e.g., Cassandra)

 Graph (e.g., Neo4j)

6. Designed for Big Data & Real- me Applica ons

 Built to store and process massive volumes of data with low latency.

 Widely used in analy cs, machine learning pipelines, and event processing.

7. Cloud-Na ve and Globally Distributed

 Many NoSQL solu ons (like Firebase, Cosmos DB, DynamoDB) offer built-in support for mul -region
replica on, failover, and cloud deployment.

8. Easier Object Mapping

 NoSQL databases store data in formats (e.g., JSON) that map more naturally to objects in programming
languages, simplifying development.

9. Eventual Consistency Models

 While not always ACID-compliant, NoSQL databases o en support eventual consistency models that
are acceptable and more performant for many use cases.

10. Cost-Effec ve

 Open-source and cloud-na ve op ons reduce opera onal costs.

 Horizontal scaling on commodity hardware is o en cheaper than ver cal scaling.

Summary Table

Advantage Benefit

Schema Flexibility Faster development, easier model changes

High Scalability Handles millions of users and large datasets

Performance Fast reads/writes, low latency

Big Data Support Designed for large volumes and real- me data

Mul -Model Support Fits diverse use cases (docs, key-value, graphs)

Cloud & Global Readiness Mul -region, high availability


Various models available in NoSQL

Various Data Models in NoSQL Databases

NoSQL databases are classified based on how they store and model data. Each model is suited for specific use
cases and types of queries.

1. Document-Oriented Model

 Structure: Stores data as documents (usually JSON, BSON, or XML).

 Use Case: Content management systems, product catalogs, user profiles.

 Key Feature: Flexible schema, nested data, easy to map with app objects.

Examples:

 MongoDB

 CouchDB

 Firebase Firestore

 Amazon DocumentDB

Example Document:

{ "name": "Alice", "email": "alice@example.com", "skills": ["Python", "MongoDB"] }

2. Key-Value Model

 Structure: Data is stored as a key and value pair.

 Use Case: Caching, session management, real- me lookups.

 Key Feature: Extremely fast, simple reads/writes.

Examples:

 Redis

 Amazon DynamoDB

 Riak

 Berkeley DB

Example:

"user123": { "name": "Alice", "email": "alice@example.com" }

3. Column-Family Model (Wide-Column Store)

 Structure: Stores data in tables, rows, and columns, but columns are grouped into families.
 Use Case: Time-series data, analy cs, IoT, big data workloads.

 Key Feature: Fast for queries over large datasets; highly scalable.

Examples:

 Apache Cassandra

 HBase

 ScyllaDB

 Google Bigtable

Example:

Row Key: user123

| name | email | age |

| Alice | alice@example.com | 25 |

4. Graph Model

 Structure: Data is represented as nodes (en es) and edges (rela onships).

 Use Case: Social networks, recommenda on engines, fraud detec on.

 Key Feature: Fast rela onship queries, traversals.

Examples:

 Neo4j

 ArangoDB

 OrientDB

 Amazon Neptune

Example:

(Alice) ---[FRIEND_OF]---> (Bob)

5. Mul -Model Databases

 Structure: Support mul ple NoSQL models (e.g., document + graph).

 Use Case: Complex applica ons requiring mul ple data representa ons.

 Key Feature: Flexible development in a single database engine.

Examples:

 ArangoDB (Document + Graph + Key-Value)


 OrientDB (Document + Graph + Object)

 Couchbase (Document + Key-Value)

Summary Table

NoSQL Model Data Format Best For Example DBs

Document Store JSON, BSON CMS, user data, catalogs MongoDB, CouchDB

Key-Value Store Key => Value Caching, sessions, fast lookup Redis, DynamoDB

Column Store Rows + Columns Big data, analy cs Cassandra, HBase

Graph DB Nodes + Edges Social, fraud, recommenda ons Neo4j, Amazon Neptune

Mul -Model Mixed formats Complex & diverse use cases ArangoDB, OrientDB

Demonstrate the crea on and retrieval car models in Cassandra database. Use key space, column family.

Example: Crea ng and Retrieving Car Models in Cassandra

Step 1: Create a Keyspace

A keyspace in Cassandra is similar to a database in RDBMS.

CREATE KEYSPACE car_dealership WITH replica on = { 'class': 'SimpleStrategy', 'replica on_factor': 1 };

SimpleStrategy is good for development. In produc on, use NetworkTopologyStrategy.

Step 2: Use the Keyspace

USE car_dealership;

Step 3: Create a Column Family (Table)

Create a table to store car model informa on.

CREATE TABLE car_models ( model_id UUID PRIMARY KEY, brand TEXT, model_name TEXT, year INT, fuel_type
TEXT, price DECIMAL );

 model_id: Unique iden fier for each car model.

 PRIMARY KEY: Ensures each row is uniquely iden fied.

Step 4: Insert Data into the Table

INSERT INTO car_models (model_id, brand, model_name, year, fuel_type, price) VALUES (uuid(), 'Toyota',
'Corolla', 2021, 'Petrol', 1500000.00);

INSERT INTO car_models (model_id, brand, model_name, year, fuel_type, price) VALUES (uuid(), 'Tesla', 'Model
3', 2022, 'Electric', 3500000.00);
INSERT INTO car_models (model_id, brand, model_name, year, fuel_type, price) VALUES (uuid(), 'Hyundai',
'Creta', 2023, 'Diesel', 1800000.00);

🛠 uuid() generates a unique ID for each entry.

Step 5: Retrieve All Car Models

SELECT * FROM car_models;

Sample Output

model_id brand model_name year fuel_type price

3a1f...e1df Toyota Corolla 2021 Petrol 1500000.00

b5c2...9a1a Tesla Model 3 2022 Electric 3500000.00

8e4d...1b2f Hyundai Creta 2023 Diesel 1800000.00

Notes

 Cassandra does not support joins or complex transac ons — data is usually denormalized.

 Querying requires knowledge of the primary key or indexed columns.

 Use ALLOW FILTERING with cau on; it's inefficient on large datasets.

Explain Document Database and its Vendor Packages in detail.

Document-Oriented Database: Concepts and Vendor Packages (Detailed Guide)

What is a Document-Oriented Database?

A document-oriented database is a type of NoSQL database that stores data as documents—usually in


formats like JSON, BSON, or XML. Unlike tradi onal rela onal databases that use tables and rows, document
databases store data in semi-structured, self-describing documents.

Key Concepts of Document Databases

Concept Descrip on

Document A unit of data stored in a flexible structure (like JSON or BSON)

Collec on A group of related documents (similar to a table in RDBMS)

Schema-less Each document can have a different structure, offering high flexibility

Nested Data Documents can contain nested fields, arrays, and objects

Indexing Documents can be indexed on any field to enhance search performance

Example Document
{ "_id": "u123", "name": "Alice", "email": "alice@example.com", "skills": ["Java", "MongoDB"], "address": {
"city": "Bangalore", "zip": "560001" } }

Each document is:

 Self-contained

 Human-readable

 Easily mapped to programming language objects

Advantages of Document Databases

 Schema flexibility – allows easy evolu on of the data model

 High performance for read/write-heavy applica ons

 Natural integra on with web apps using JSON

 Horizontal scalability – supports distributed, cloud-na ve architecture

 Great for semi-structured or nested data

Major Document Database Vendors and Packages

Vendor Product Descrip on

MongoDB MongoDB Open-source, most popular document DB using BSON format. Offers
Inc. indexing, aggrega on, and full CRUD support.

Apache CouchDB Uses JSON to store documents, features built-in replica on and RESTful
interface.

Amazon AWS Amazon Fully managed MongoDB-compa ble document DB, integrated with
DocumentDB AWS ecosystem.

Google Cloud Firestore Real- me, cloud-hosted NoSQL database op mized for mobile/web
(Firebase) apps.

Couchbase Couchbase Combines document store with key-value access and in-memory
Inc. caching. Supports SQL-like queries via N1QL.

Microso Cosmos DB Globally distributed, mul -model DB service suppor ng MongoDB API,
Azure SQL API, Gremlin, etc.

MarkLogic MarkLogic Server Enterprise-grade document database suppor ng XML/JSON, ACID


transac ons, and seman c data.

RavenDB RavenDB Open-source document DB with ACID support, full-text search, and
clustering.
OrientDB OrientDB Mul -model database (document + graph + key-value + object). Open
source.

Feature Comparison of Top Vendors

Feature MongoDB CouchDB Firestore Cosmos DB

Format BSON JSON JSON JSON

Query Language MongoQL REST API Firebase SDK SQL-like, MongoDB API

Replica on Replica sets Master-master Built-in Global automa c

ACID Compliance Par al Yes (on document) Yes Yes

Best Use Case General purpose Offline-first apps Mobile/web apps Distributed apps

Use Cases

 Content management systems

 E-commerce catalogs

 User profiles and authen ca on

 IoT sensor data

 Real- me chat/messaging apps

 Mobile app backends

Summary

Property Document Database

Structure JSON / BSON / XML documents

Flexibility Schema-less

Query Support Rich query, indexing, aggrega on

Best Suited For Semi-structured, nested data

Horizontal Scalability Yes

Data Access Via fields inside documents

Real-World Examples MongoDB, CouchDB, Firestore, Cosmos DB

Explain graph database with vendor packages in detail.


What is a Graph Database?

A graph database is a type of NoSQL database designed to store and process data with complex
rela onships using graph structures. It consists of:

 Nodes: En es (like people, places, or products)

 Edges (Rela onships): Connec ons between en es

 Proper es: Key-value pairs associated with nodes and edges

Why Graph Databases?

Tradi onal rela onal databases struggle with deep joins and rela onship-intensive queries, while graph
databases are designed to efficiently model and query connected data.

Core Concepts of Graph Databases

Component Descrip on

Node Represents an en ty (e.g., user, product)

Edge Represents a rela onship (e.g., FRIEND_OF)

Property Key-value metadata a ached to nodes or edges

Label Describes a node type (e.g., :Person)

Graph En re network of nodes and rela onships

Example Graph Structure

(Alice) ---[FRIEND_OF]---> (Bob)

[LIKES]

(Product A)

This models:

 Alice is friends with Bob

 Alice likes Product A

Advantages of Graph Databases

Advantage Descrip on
Fast Rela onship Queries Op mized for traversing rela onships quickly

Schema Flexibility No fixed schema required

Intui ve Data Modeling Data mirrors real-world rela onships

Agile and Scalable Easily evolves with your applica on

Powerful Query Languages Cypher, Gremlin, SPARQL allow complex traversals

Popular Graph Database Vendors & Packages

Vendor Graph DB Descrip on

Neo4j Inc. Neo4j Industry-leading graph DB, uses Cypher query language. Great for
real- me recommenda on engines, fraud detec on.

Apache TinkerPop (Gremlin) Framework for graph processing, supports mul ple backend engines
like JanusGraph.

Amazon Amazon Neptune Fully managed graph DB suppor ng both property graphs (Gremlin)
AWS and RDF (SPARQL).

Microso Azure Cosmos DB Supports graph storage via Gremlin; part of mul -model Cosmos DB.
(Gremlin API)

OrientDB OrientDB Mul -model DB that supports graph + document + object models.

ArangoDB ArangoDB Mul -model DB (graph, document, key-value), supports AQL and
graph traversals.

TigerGraph TigerGraph High-performance na ve parallel graph DB for big data analy cs.

TerminusDB TerminusDB Git-like graph database focused on collabora on and versioned data.

Comparison of Top Graph DBs

Feature Neo4j Amazon Neptune OrientDB ArangoDB

Query Language Cypher SPARQL, Gremlin SQL-like AQL

Deployment Self-hosted, Cloud Managed AWS Self-hosted Self-hosted/Cloud

ACID Compliance Yes Yes Par al Yes

Mul -Model Support No No Yes Yes

Best Use Case Real- me traversal Knowledge Graphs Hybrid apps Graph + JSON apps
Sample Query (Neo4j – Cypher Language)

// Create two nodes and a rela onship CREATE (a:Person {name: "Alice"}) CREATE (b:Person {name: "Bob"})
CREATE (a)-[:FRIEND_OF]->(b);

// Query all friends of Alice MATCH (a:Person {name: "Alice"})-[:FRIEND_OF]->(friends) RETURN friends.name;

Use Cases of Graph Databases

Use Case Descrip on

Social Networks Model friendships, followers, and posts

Recommenda on Engines Suggest items based on user behavior and interests

Fraud Detec on Detect suspicious pa erns and loops in transac ons

Knowledge Graphs Store and traverse seman c data

Network/IT Management Model infrastructure, connec vity, dependencies

Summary

Property Graph Database

Data Structure Nodes + Edges + Proper es

Schema Flexible / schema-less

Strength Complex rela onship modeling and traversal

Performance High for deep-link queries

Examples Neo4j, Amazon Neptune, OrientDB, ArangoDB

Explain Key-Value Database and its Vendor Packages in detail.

What is a Key-Value Database?

A Key-Value Database is the simplest form of NoSQL database. It stores data as a collec on of key-value
pairs, where:

 The key is a unique iden fier.

 The value can be any data (string, number, JSON, blob, etc.).

Key Concepts

Component Descrip on
Key A unique iden fier (like a primary key)

Value The actual data associated with the key (can be simple or complex)

Schema-less There is no fixed schema; values can vary in type and structure

High Performance Designed for ultra-fast reads and writes

Example

{ "user:1001": { "name": "Alice", "age": 25 }, "user:1002": { "name": "Bob", "age": 30 }, "session:xyz123":


"ac ve" }

Each record is accessed via its unique key, and the value can be any serializable object or blob.

Advantages of Key-Value Databases

Advantage Descrip on

Extreme Speed Op mized for very fast read/write opera ons

Simplicity Easy to implement and scale

Scalability Horizontally scalable across nodes

Flexibility Can store any data format, no schema restric ons

Ideal for Caching Great for session data, shopping carts, etc.

Popular Key-Value Database Vendors & Packages

Vendor Product Descrip on

Redis Labs Redis In-memory key-value store. Fast, supports strings, hashes, lists, sets,
pub/sub, and TTL. Widely used for caching, queues, and real- me
analy cs.

Amazon Amazon Fully managed key-value and document DB with automa c scaling and
AWS DynamoDB mul -region replica on.

Microso Azure Table Key-value database for structured NoSQL data, integrated with Azure
Azure Storage cloud.

Riak Riak KV Highly available, fault-tolerant distributed key-value database. Ideal for IoT
and large-scale systems.

Etcd etcd Lightweight, distributed key-value store for configura on and service
discovery (used in Kubernetes).
LevelDB LevelDB High-performance embedded key-value store by Google. Used in browsers
and low-level systems.

Berkeley DB Berkeley DB Embedded key-value database library developed by Oracle. O en used in


mobile or embedded systems.

Aerospike Aerospike High-performance, scalable key-value database with hybrid memory/disk


storage. Ideal for ad-tech, finance, and fraud detec on.

Key-Value vs Other NoSQL Models

Feature Key-Value DB Document DB Column Store Graph DB

Structure Key-Value pairs JSON-like Docs Rows/Columns Nodes & Edges

Query Key-based lookup Field-based Wide queries Rela onship-based

Schema None Flexible Semi-structured Flexible

Use Case Caching, sessions CMS, profiles Analy cs Social networks

Sample Redis Opera ons (CLI)

# Set a value SET user:1001 "Alice" # Get a value GET user:1001

# Store JSON-like data HSET user:1002 name "Bob" age "30"

# Retrieve all fields HGETALL user:1002

Use Cases

Use Case Descrip on

Caching Store frequently accessed data (e.g., product details)

Session Management Maintain user sessions in web applica ons

Shopping Cart Store cart items temporarily per user

IoT Data Inges on Store me-sensi ve data with high throughput

Leaderboard/Ranking Real- me counters, sorted sets

Pub/Sub Systems Messaging systems using Redis or similar

Summary

Property Key-Value Database


Structure Simple key-value pairs

Schema No schema (schema-less)

Speed Extremely fast (low latency)

Scalability High, supports horizontal scaling

Common Products Redis, DynamoDB, Riak, etcd

Best Use Cases Caching, sessions, real- me data

Mongo DB Drivers

What is a MongoDB Driver?

A MongoDB driver is a library or package that allows applica ons to connect, interact, and communicate with
a MongoDB server using language-specific APIs.

Each driver provides:

 Connec on management

 CRUD opera ons

 Query building

 BSON serializa on/deserializa on

 Support for MongoDB features (aggrega on, indexes, etc.)

Official MongoDB Drivers

Language Driver Name Package/Install Command

Node.js mongodb npm install mongodb

Python pymongo pip install pymongo

Java mongodb-driver-sync Available via Maven/Gradle

C# / .NET MongoDB.Driver Install-Package MongoDB.Driver (NuGet)

C++ mongocxx Part of MongoDB C++ driver (libmongocxx)

Go mongo-go-driver go get go.mongodb.org/mongo-driver/mongo

PHP mongodb (extension + library) pecl install mongodb + Composer for mongodb/mongodb

Ruby mongo gem install mongo

Scala mongo-scala-driver SBT: "org.mongodb.scala" %% "mongo-scala-driver"


Swi MongoSwi Available via Swi Package Manager

Rust mongodb crate Add mongodb = "X.X" in Cargo.toml

Perl MongoDB cpan MongoDB or cpanm MongoDB

Example: Python (PyMongo)

from pymongo import MongoClient client = MongoClient("mongodb:

//localhost:27017/") db = client["car_dealership"] collec on = db["car_models"]

# Insert a document collec on.insert_one({"brand": "Toyota", "model": "Corolla", "year": 2023})

# Query documents for car in collec on.find(): print(car)

Community & Third-Party Drivers

MongoDB also has:

 Community-supported drivers (for Erlang, Haskell, ColdFusion, etc.)

 ODM/ORM frameworks like:

 Mongoose (Node.js ODM)

 MongoEngine (Python ORM-like ODM)

 Morphia (Java ODM)

Summary

Feature Descrip on

Language Support Extensive official driver support for major languages

Ease of Use Abstracts raw queries into API calls

Integra on Compa ble with modern frameworks and pla orms

Security Supports authen ca on, SSL/TLS, and access controls

Storing Data In and Accessing Data from MongoDB

1. Storing Data in MongoDB

You can store data using the MongoDB shell, Compass GUI, or programma cally via drivers.

A. Using MongoDB Shell (MongoDB CLI)

// Connect to MongoDB and select/create a database use car_dealership;


// Create a collec on and insert a document db.car_models.insertOne({ brand: "Toyota", model: "Corolla",
year: 2023, fuel_type: "Petrol", price: 1500000 });

// Insert mul ple documents db.car_models.insertMany([ { brand: "Tesla", model: "Model 3", year: 2022,
fuel_type: "Electric", price: 3500000 }, { brand: "Hyundai", model: "Creta", year: 2021, fuel_type: "Diesel",
price: 1800000 } ]);

B. Using Python (with pymongo)

from pymongo import MongoClient # Connect to MongoDB client = MongoClient("mongodb:

//localhost:27017/") db = client["car_dealership"] collec on = db["car_models"]

# Insert one document collec on.insert_one({ "brand": "Honda", "model": "Civic", "year": 2023, "fuel_type":
"Petrol", "price": 2200000 })

# Insert mul ple documents collec on.insert_many([ {"brand": "Ford", "model": "Mustang", "year": 2021,
"fuel_type": "Petrol", "price": 5000000}, {"brand": "Mahindra", "model": "Thar", "year": 2023, "fuel_type":
"Diesel", "price": 1600000} ])

2. Accessing Data from MongoDB

A. Using MongoDB Shell

// Retrieve all documents db.car_models.find();

// Filter documents db.car_models.find({ brand: "Tesla" });

// Pre y print db.car_models.find().pre y();

B. Using Python (Read from MongoDB)

# Find all documents for car in collec on.find(): print(car)

# Find documents with a filter results = collec on.find({"fuel_type": "Petrol"}) for car in results: print(car)

# Find one car = collec on.find_one({"brand": "Ford"}) print(car)

3. Upda ng & Dele ng (Op onal)

Update Example

// Update price of a Tesla db.car_models.updateOne({ brand: "Tesla" }, { $set: { price: 3600000 } });

Delete Example

// Delete a Hyundai car db.car_models.deleteOne({ brand: "Hyundai" });

Summary Table

Opera on Mongo Shell Example Python Example (PyMongo)


Insert One insertOne({...}) collec on.insert_one({...})

Insert Many insertMany([...]) collec on.insert_many([...])

Find All find() collec on.find()

Find with find({brand: "Toyota"}) collec on.find({"brand": "Toyota"})


Filter

Update updateOne({model: "Civic"}, {$set: collec on.update_one({"model": "Civic"}, {"$set":


{...}}) {...}})

Delete deleteOne({...}) collec on.delete_one({...})

Querying MongoDB

Basic Query Structure

MongoDB queries follow this format:

db.<collec on_name>.find(<filter>, <projec on>);

 filter: Criteria to select documents.

 projec on: Fields to include or exclude.

1. Retrieve All Documents

db.car_models.find();

Output: All documents in the car_models collec on.

2. Filter Documents (WHERE Clause)

db.car_models.find({ brand: "Toyota" });

Output: All cars with brand "Toyota"

3. Project Specific Fields

db.car_models.find( { brand: "Toyota" }, { model: 1, year: 1, _id: 0 } );

Output: Show only model and year of Toyota cars (hide _id).

4. Comparison Operators

Operator Meaning Example


$gt Greater than { price: { $gt: 2000000 } }

$lt Less than { year: { $lt: 2023 } }

$gte Greater than or equal { year: { $gte: 2020 } }

$ne Not equal { fuel_type: { $ne: "Diesel" } }

db.car_models.find({ price: { $gt: 2000000 } });

5. Logical Operators

db.car_models.find({ $or: [ { fuel_type: "Electric" }, { fuel_type: "Hybrid" } ] });

Other logical operators: $and, $not, $nor

6. Sor ng Results

db.car_models.find().sort({ price: -1 }); // Descending db.car_models.find().sort({ year: 1 }); // Ascending

7. Limi ng and Skipping Results

db.car_models.find().limit(5); // First 5 results db.car_models.find().skip(10); // Skip first 10


db.car_models.find().limit(5).skip(10); // Pagina on

8. Using Regular Expressions

db.car_models.find({ model: /Model/i });

Output: Models containing the word "Model" (case-insensi ve)

9. Array Queries (if value contains arrays)

If documents have:

{ "features": ["sunroof", "bluetooth", "camera"] }

You can query:

db.car_models.find({ features: "camera" });

Output: Finds documents where features array includes "camera"

10. Aggrega on Queries (Advanced)

db.car_models.aggregate([ { $match: { fuel_type: "Petrol" } }, { $group: { _id: "$brand", avg_price: { $avg:


"$price" } } } ]);

Output: Average petrol car price per brand


Summary Table

Query Type Example

Find all db.car_models.find()

Filter by value db.car_models.find({ brand: "Tesla" })

Project fields db.car_models.find({}, { brand: 1, model: 1, _id: 0 })

Comparison db.car_models.find({ year: { $gte: 2021 } })

Logical db.car_models.find({ $or: [{ brand: "Tesla" }, { fuel_type: "Electric" }] })

Sort db.car_models.find().sort({ price: -1 })

Limit/Skip db.car_models.find().limit(5).skip(5)

Regex db.car_models.find({ model: /Creta/i })

Aggrega on db.car_models.aggregate([...])

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy