No SQL
No SQL
NoSQL databases
• Databases, which store data in a format different from relational databases, are known
as NoSQL databases.
• NoSQL stands for “not only SQL,” i.e. either the database can store and manage data
using “no SQL” or it can work in a combination that combines the flexibility of the
newer approach (NoSQL) with the power of the traditional relational system (SQL).
• Unlike relational databases, related data doesn’t have to be split up between multiple
tables; instead, it can be nested within the same data structure.
• NoSQL is an umbrella term to describe any alternative system to traditional SQL
databases.
• NoSQL databases are all quite different from SQL databases.
• NoSQL use a data model that has a different structure than the traditional row-and-
column table model used with relational database management systems (RDBMS).
NoSQL database features
Each NoSQL database has its own unique features. At a high level, many NoSQL
databases have the following features:
• Flexible schemas
• Horizontal scaling
• Fast queries due to the data model
• Ease of use for developers
1. Flexible Schema
• Unlike SQL databases, where you must determine and declare a table's schema before
inserting data, NoSQL dB, by default, do not require their documents to have the same
schema. That is:
• The documents in a single collection do not need to have the same set of fields and the data type for a
field can differ across documents within a collection.
• To change the structure of the documents in a collection, such as add new fields, remove existing fields,
or change the field values to a new type, update the documents to the new structure.
Vertical scaling refers to increasing Horizontal scaling, also known as scale-out, refers to bringing
the processing power of a single on additional nodes to share the load.
server or cluster.
• Both relational and non-relational databases can scale up, but eventually, there will be a limit in terms of
maximum processing power and throughput.
• Additionally, there are increased costs with scaling up to high-performing hardware, as costs do not scale linearly.
• Horizontal Scaling is difficult with relational databases due to the difficulty in spreading out related data across
nodes.
• With non-relational databases, this is made simpler since collections are self-contained and not coupled
relationally
• Scaling MongoDB horizontally is achieved through Sharding and Replica sets.
• Replication allows for high availability, redundancy/failover handling, and decreased bottlenecks on
read operations.
• However, they can also introduce issues for applications with large amounts of write transactions, as
each update must be propagated over to every replica set member.
Relational Database
NoSQL
Types of NoSQL databases
• Key-Value Pairs databases are best suited for the following cases:
1. Storing session information: offers to save and restore sessions.
2. User preferences: Specific Data for a particular user
3.Shopping carts: easily handle the loss of storage nodes and quickly scale Big data
during a holiday/sale on an e-commerce application.
4.Product recommendations: offering recommendations based on the person’s data.
• Pros:
– Simple & Powerful Data model
– Scalable
– Open Formats
– No foreign Keys
• Cons:
– Not suitable for relational data
– Querying limited to keys & indexes
– Map Reduce for more significant queries
Document Stores: Use Case
1.User Profiles: Since they have a flexible Schema, the document can store different
attributes and values. This enables the users to store different types of information.
2.Management of Content: Since it has a flexible schema, collection and storing any
data has been made possible. This allows the creation of new types of content,
including images, videos, comments, etc. Everyday use is seen in blogging
platforms.
• Some popular Document stores are MongoDB, CouchDB, Lotus Notes.
Graph Databases
• Examples of dB: MySQL, PostgreSQL, Oracle, SQLite, and Microsoft SQL Server.
The BASE Model
BASE stands for:
• Basically Available – Rather than enforcing immediate consistency, BASE-modelled NoSQL databases
will ensure availability of data by spreading and replicating it across the nodes of the database cluster.
• Soft State – Due to the lack of immediate consistency, data values may change over time. The BASE
model breaks off with the concept of a database which enforces its own consistency, delegating that
responsibility to developers.
• Eventually Consistent – The fact that BASE does not enforce immediate consistency does not mean
that it never achieves it. However, until it does, data reads are still possible (even though they might not
reflect the reality).
12. Examples Oracle, MySQL, SQL Server, etc. DynamoDB, Cassandra, CouchDB, SimpleDB etc.
MongoDB
• MongoDB is an open-source database that uses a document-oriented data model and a non-
structured query language.
• In the MongoDB data model, it is easy to split data among several servers.
• MongoDB is a distributed database at its core, so high availability, horizontal scaling, and
geographic distribution are built in and easy to use
• It automatically redistributes documents, balances data, and loads a cluster, and routes the user
requests to the right machine.
• MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document
to document and data structure can be changed over time
• The document model maps to the objects in your application code, making data easy to work with.
• Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze
your data.
Need MongoDB technology.
• MongoDB technology overcame one of the biggest pitfalls of the traditional database
systems, that is, scalability. MongoDB has exceptional scalability.
• No downtime while the application is being scaled
• It makes it easy to fetch the data and provides continuous and automatic integration.
• Performs in-memory processing
• Text search
• Graph processing
• Global replication
• Economical
• MongoDB provides the right mix of technology and data for competitive advantage.
• It is most suited for mission-critical applications since it considerably reduces risks.
• It increasingly accelerated the time to value (TTV) and lowered the total cost of ownership.
• It builds applications that are just not possible with traditional relational databases.
Architecture of MongoDB NoSQL Database
The following are the components of MongoDB architecture:
• Database
• Collection
• Document
Database
It is also called the physical container for data. Every database
has its own set of files existing on the file system. In a single
MongoDB server, there are multiple databases present.
Collection
The collection consists of various documents from different fields. All the collections reside within one database.
In collections no schemas are present.
Document
The set of key values are assigned to the document which is in turn associated with dynamic schemas. The benefit
of using these schemas is that a document may not have to possess the same fields whereas they can have different
data types.
Features of MongoDB
• Queries support: MongoDB supports ad-hoc and document-based queries.
• Index Support: All fields in the document are indexed.
• Replication: MongoDB possesses Master-Slave replication. It uses a native application to preventing
database downtime by maintaining multiple copies of data.
• Multiple Servers: Duplicate data that is stored in the database is run over multiple servers to avoid the
damage caused due to hardware failure.
• Auto-sharding: The data is being distributed among several physical partitions known as shards.
MongoDB has an in-built feature called automatic load balancing.
• MapReduce: It is a flexible aggregation tool that supports the MapReduce function.
• Failure Handling: In MongoDB, works effectively in case of failures such as multiple machine failures,
data center failures by protecting data and making it available.
• GridFS: This feature will allow the files to divide into smaller parts and store them in different documents
without complicating the stack.
• Schema-less Database: MongoDB is a schema-less database programmed in C++ language.
• Document-oriented Storage: It uses BSON format which is similar to JSON
• Procedures: MongoDB JavaScript works better than procedures as databases use the language more than
procedures.
Drawbacks of MongoDB
• It uses high memory for data storage
• Document size has a limit
• Less flexibility with querying
• There is no transaction support