PYTHON Tutorial

Introduction to NoSQL DB

What is NoSQL?

NoSQL (Not Only SQL) databases are a non-relational alternative to traditional relational databases. They were developed to handle large volumes of unstructured or semi-structured data that do not fit well into the tabular structure of relational databases.

Types of NoSQL Databases

  • Key-value stores: Store data as pairs of keys and values, such as Redis, DynamoDB.
  • Document databases: Store documents as JSON or XML, such as MongoDB, CouchDB.
  • Column family databases: Store data as columns within column families, such as Apache Cassandra, HBase.
  • Graph databases: Store data as nodes and edges, which can represent relationships between data points, such as Neo4j, MongoDB Realm.

Benefits of NoSQL

  • Scalability: Can handle large amounts of data without performance degradation.
  • Flexibility: Can store different types of data and adapt to changing data models.
  • Performance: Optimized for specific types of queries and data operations.
  • Cost-effectiveness: Often cheaper than relational databases to maintain.

Python Example:

Overview of NoSQL Databases

import pymongo
import redis

# Connect to a MongoDB document database
client = pymongo.MongoClient()
db = client.mydb
collection = db.mycollection

# Insert a document into the collection
document = {"name": "John", "age": 30}
collection.insert_one(document)

# Connect to a Redis key-value store
r = redis.Redis()

# Set a key-value pair
r.set("name", "John")

# Get the value associated with a key
value = r.get("name")

# Print the results
print(value)

This example demonstrates how to store and retrieve data in a MongoDB document database and a Redis key-value store, highlighting their differences compared to relational databases.