What is NoSQL?
NoSQL databases (AKA "not only SQL") store data differently than relational tables. NoSQL databases come in a variety of types based on their data model. The main types are document, key-value, wide-column, and graph. They provide flexible schemas and scale easily with large amounts of big data and high user loads.
In this article, you'll learn what a NoSQL database is, why (and when!) you should use one, and how to get started.
Table of contents
- What is a NoSQL Database?
- Types of NoSQL Database
- Brief History of NoSQL Databases
- NoSQL Database Features
- Relational database vs NoSQL database example
- Differences between RDBMS and NoSQL databases
- NoSQL use cases
- When should NoSQL be Used?
- NoSQL Database Misconceptions
- NoSQL Query Tutorial
- Summary
- FAQs
What is a NoSQL database?
When people use the term “NoSQL database,” they typically use it to refer to any non-relational database. Some say the term “NoSQL” stands for “non-SQL” while others say it stands for “not only SQL.” Either way, most agree that NoSQL databases store data in a more natural and flexible way. NoSQL, as opposed to SQL, is a database management approach, whereas SQL is just a query language, similar to the query languages of NoSQL databases.
Types of databases — NoSQL
Over time, four major types of NoSQL databases have emerged: document databases, key-value databases, wide-column stores, and graph databases. Nowadays, multi-model databases are also becoming quite popular.
Document-oriented databases
A document-oriented database stores data in documents similar to JSON (JavaScript Object Notation) objects. Each document contains pairs of fields and values. The values can typically be a variety of types, including things like strings, numbers, booleans, arrays, or even other objects. A document database offers a flexible data model, much suited for semi-structured and typically unstructured data sets. They also support nested structures, making it easy to represent complex relationships or hierarchical data.
Examples of document databases are MongoDB and Couchbase. A typical document will look like the following:
1{
2"_id": "12345",
3"name": "foo bar",
4"email": "foo@bar.com",
5"address": {
6"street": "123 foo street",
7"city": "some city",
8"state": "some state",
9"zip": "123456"
10},
11"hobbies": ["music", "guitar", "reading"]
12}
Key-value databases
A key-value store is a simpler type of database where each item contains keys and values. Each key is unique and associated with a single value. They are used for caching and session management and provide high performance in reads and writes because they tend to store things in memory. Examples are Amazon DynamoDB and Redis. A simple view of data stored in a key-value database is given below:
1Key: user:12345
2Value: {"name": "foo bar", "email": "foo@bar.com", "designation": "software developer"}
Wide-column stores
Wide-column stores store data in tables, rows, and dynamic columns. The data is stored in tables. However, unlike traditional SQL databases, wide-column stores are flexible, where different rows can have different sets of columns. These databases can employ column compression techniques to reduce the storage space and enhance performance. The wide rows and columns enable efficient retrieval of sparse and wide data. Some examples of wide-column stores are Apache Cassandra and HBase. A typical example of how data is stored in a wide-column is as follows:
name | id | dob | city | |
---|---|---|---|---|
Foo bar | 12345 | foo@bar.com | Some city | |
Carn Yale | 34521 | bar@foo.com | 12-05-1972 |
Graph databases
A graph database stores data in the form of nodes and edges. Nodes typically store information about people, places, and things (like nouns), while edges store information about the relationships between the nodes. They work well for highly connected data, where the relationships or patterns may not be very obvious initially. Examples of graph databases are Neo4J and Amazon Neptune. MongoDB also provides graph traversal capabilities using the $graphLookup stage of the aggregation pipeline. Below is an example of how data is stored: