Introduction to NoSQL and MongoDB
Topics
- What Is NoSQL?
- NoSQL vs SQL
- Why NoSQL?
- TYPES of NO SQL
- CAP THEOREM
- Usage of NoSQL Databases at scale
- MongoDB
NoSQL stands for “Not Only SQL”.
NoSQL database environment is, simply put, a non
relational, schema-less and largely distributed database.
Developed in late 2000s to deal with limitations of SQL databases.
What is NoSQL
NoSQL vs SQL
- free from joins and relationships
- lower maintenance cost compared to RDBMS
- NoSQL increases the need for developers and database designers
- Structure and data types are fixed in advance in RDBMS
Why NoSQL
Agility
- SQL databases do not support agile development very well due to their fixed data model.
- A core principle of agile development is adapting to evolving application requirements.
Handling Unstructured Data
- overhead of joins and maintaining relationships amongst various data. Also joins are expensive.
Why NoSQL
Scaling
- Scaling vertically vs horizontally.
- Scaling vertically meaning a single server must be made increasingly powerful in order to deal with increased demand.
- Vertically scaling SQL databases requires significant additional engineering, and core relational features such as JOINs, referential integrity and transactions are typically lost.
- Horizontal scaling means to addmore commodity servers or cloud instances.
Why NoSQL
Auto-sharding
- The solution to support rapidly growing applications is to scale horizontally, by adding servers instead of concentrating more capacity in a single server.
- NoSQL databases, on the other hand, usually support auto-sharding, meaning that they natively and automatically spread data across an arbitrary number of servers
PolyGlot Persistence
- Polyglot Persistence means that when storing data, it is best to use multiple data storage technologies, chosen based upon the way data is being used
- It’s the same idea behind Polyglot Programming
Data Storage Needs
- The rise of the web as a platform
- Relational databases were not designed to run efficiently on clusters.
- Ensuring global availability is difficult for relational databases where separate add-ons are required – which increases complexity.
Why NoSQL
The Shift to the Digital Economy option
- mobile games can reach tens of millions of users in a matter of months e.g. Pokemon GO, Clash of Clans etc
- With a distributed, scale-out database, mobile applications can start with a small deployment and expand as the user base grows, rather than deploying an expensive, large relational database server from the beginning.
Why NoSQL
Internet of Things (IoT)
- some 20 billion devices are connected to the Internet
- The volume, velocity of data is increasing which is semi-structured and continuous
- Relational databases struggle with the three well-known challenges from big data IoT applications: Scalability, Throughput, and data variety.
Why NoSQL
The Shift to the Digital Economy
- mobile games can reach tens of millions of users in a matter of months e.g. Pokemon GO, Clash of Clans etc
- With a distributed, scale-out database, mobile applications can start with a small deployment and expand as the user base grows, rather than deploying an expensive, large relational database server from the beginning.
Why NoSQL
Personalization
Integrated Caching
Analytics and Business Intelligence (Big Data)
Types of NoSQL Databases
1. Document based Database
- Instead of using tables and rows as in relational databases, Document Stored NoSQL databases are built on an architecture of collections and documents.
- Documents are stored in XML / JSON / BSON
- Document databases are essentially the next level of key-value, allowing nested values associated with each key.
- Document databases are generally useful for CMS, blogging platforms, real-time analytics, ecommerce-applications
- examples include MongoDB, RethinkDB, CouchDB
Types of NoSQL Databases
2. Key-value Pairs
- Similar to a hash table where there is a unique key and a pointer to a particular item of data. the value is usually BLOBs.
- Redis, Riak, Local Storage (browser) and Amazon DynamoDB are examples of key-value NoSQL databases.
- Key-value databases are generally useful for storing session information, user profiles, preferences, shopping cart data.
- We would avoid using Key-value databases when we need to query by data.
Types of NoSQL Databases
3. Graph Store
-
Graph stores are used to store information about networks of data, such as social connections. Graph stores include Neo4J and Giraph.
4. Column based
-
There are still keys but they point to multiple columns.
-
These were created to store and process very large amounts of data distributed over many machines.
-
Examples of column-oriented databases are Cassandra and HBase.
CAP Theorem
- DBMS follows the ACID property. Eric Brewer put forth the CAP theorem which states that in any distributed system we can choose only two of consistency, availability or partition tolerance.
- Consistency – once data is written, all future read requests will contain that data.
- Availability – the database is always available and responsive.
- Partition tolerance – if one part of the database is unavailable, other parts are unaffected.
Usage of NoSQL DBs at scale
- RocksDB - Embeddable persistent key-value store.
- Cassandra - Column-based database
- Memcached - An in-memory key-value store
Pokemon GO
- Google Cloud Datastore’s NoSQL database.
Clash of Clans
- DynomoDB Amazon Web Services(AWS)
MongoDB
- document store with BSON (extension of JSON) type system.
- Schema-free structure
- CP in CAP Theorem
- To connect to a MongoDB server, use Mongo client (not HTTP)
- Values may contain documents, arrays and arrays of documents
- Joins or cross collection queries are not available.
MongoDB
MongoDB
Sample schema
MongoDB CRUD Operations
Create
MongoDB CRUD Operations
Read
In Mongoose
User.find({
user_id: "123"
})
.then(docs => {
socket.emit("sync success", data);
})
.catch(err => {
console.log("err", err.stack);
});
MongoDB CRUD Operations
Update
MongoDB CRUD Operations
Delete
MongoDB Relationships
-
Embedded Documents
-
References
Embedded Documents
An embedded document is when one document (often a structured text file, or a binary, or anything else is embedded within another.
MongoDB One to One Relationships
From
{
_id: "123",
name: "Leo Messi",
address: {
street: "123 Fake Street",
city: "Faketon"
}
}
To
{
_id: "123",
name: "Leo Messi"
}
{
user_id: "123",
street: "123 Fake Street",
city: "Faketon"
}
MongoDB One to Many Relationships
{
_id: "123",
name: "Leo Messi",
addresses: [
{
street: "123 Fake Street",
city: "Faketon"
},
{
street: "1 Some Other Street",
city: "Boston"
}
]
}
Introduction to NoSQL and MongoDB
By Alamgir Qazi
Introduction to NoSQL and MongoDB
Introduction to NoSQL and MongoDB. Link to notes https://github.com/alamgirqazi/IntroToNoSQL
- 1,559