NoSQL Cheat Sheet

Here’s a NoSQL cheat sheet that covers some common concepts and commands for working with NoSQL databases:

NoSQL Basics

Definition

NoSQL (Not Only SQL) databases are non-relational databases designed for flexibility and scalability.

Data Model Types

Document-oriented: Stores data in semi-structured documents (e.g., MongoDB).

Key-Value: Stores data as key-value pairs (e.g., Redis).

Column-family: Stores data in columns rather than rows (e.g., Apache Cassandra).

Graph: Stores data as nodes and edges (e.g., Neo4j).

MongoDB (Document-Oriented)

Connecting to MongoDB

mongo

Basic Commands

Show Databases:

show dbs

Use a Database:

use <database_name>

Show Collections:

show collections

CRUD Operations

Insert Document:

db.<collection_name>.insert({ key: value })

Find Documents:

db.<collection_name>.find({ key: value })

Update Document:

db.<collection_name>.update({ key: value }, { $set: { new_key: new_value } })

Delete Document:

db.<collection_name>.remove({ key: value })

Here is a dedicated MongoDB Cheat Sheet.

Redis (Key-Value)

Connecting to Redis

redis-cli

Basic Commands

Set Key-Value:

SET key value

Get Value by Key:

GET key

List All Keys:

KEYS *

Here is a dedicated Redis Cheat Wheet.

Apache Cassandra (Column-Family)

Connecting to Cassandra

  • Use cqlsh to connect to Cassandra.

Basic Commands

Create Keyspace:

CREATE KEYSPACE <keyspace_name> WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};

Create Table:

CREATE TABLE <table_name> (<column1> <datatype1>, <column2> <datatype2>, PRIMARY KEY (<primary_key_column>));

Insert Data:

INSERT INTO <table_name> (<column1>, <column2>) VALUES (<value1>, <value2>);

Query Data:

SELECT * FROM <table_name> WHERE <condition>;

Neo4j (Graph)

Connecting to Neo4j

  • Access Neo4j through the web interface.

Basic Commands

Create Node:

CREATE (:Label {key: value})

Create Relationship:

MATCH (a:Label1), (b:Label2) WHERE a.key = value1 AND b.key = value2 CREATE (a)-[:RELATIONSHIP]->(b)

Query Nodes and Relationships:

MATCH (a:Label1)-[r]->(b:Label2) WHERE a.key = value1 RETURN a, r, b

This NoSQL cheat sheet provides a basic overview of common commands for MongoDB, Redis, Apache Cassandra, and Neo4j. Note that specific commands may vary based on the database version and configuration. Always refer to the official documentation for detailed information.