Unit 4
Unit 4
### 1. **Scalability**
- **Horizontal Scaling:** NoSQL databases are designed to scale out by adding
more servers, rather than scaling up by adding more powerful servers. This makes it
easier and more cost-effective to handle increasing data volumes.
- **Elasticity:** They can dynamically adjust to varying workloads, accommodating
rapid growth in data without significant performance degradation.
### 2. **Performance**
- **High Throughput:** NoSQL databases are optimized for high-speed data
ingestion and retrieval, which is essential for real-time analytics and processing large
datasets.
- **Low Latency:** These databases offer quick read and write operations, which is
critical for applications that require fast data access and processing.
### 3. **Flexibility**
- **Schema-less Design:** NoSQL databases typically use flexible data models (e.g.,
document, key-value, wide-column, graph) that allow for dynamic changes in data
structure without requiring a predefined schema. This is beneficial for applications
with evolving data needs.
- **Variety of Data Types:** They support various data formats, including
structured, semi-structured, and unstructured data, making them suitable for diverse
data sources like social media, IoT devices, and log files.
3. Document Database:
The document database fetches and accumulates data in form of key-value pairs
but here, the values are called as Documents. Document can be stated as a complex
data structure. Document here can be a form of text, arrays, strings, JSON, XML or
any such format. The use of nested documents is also very common. It is very
effective as most of the data created is usually in form of JSONs and is
unstructured.
Advantages:
This type of format is very useful and apt for semi-structured data.
Storage retrieval and managing of documents is easy.
Limitations:
Handling multiple documents is challenging
Aggregation operations may not work accurately.
Examples:
MongoDB
CouchDB
Each of these NoSQL architectural patterns offers unique strengths tailored to specific
types of big data challenges. By choosing the appropriate NoSQL database,
organizations can effectively manage, store, and analyze their data to meet their
business objectives.
MongoDB: An introduction
Last Updated : 13 Mar, 2023
MongoDB, the most popular NoSQL database, is an open-source document-
oriented database. The term ‘NoSQL’ means ‘non-relational’. It means that
MongoDB isn’t based on the table-like relational database structure but provides
an altogether different mechanism for storage and retrieval of data. This format of
storage is called BSON ( similar to JSON format).
A simple MongoDB document Structure:
{
title: 'Geeksforgeeks',
by: 'Harshit Gupta',
url: 'https://www.geeksforgeeks.org',
type: 'NoSQL'
}
SQL databases store data in tabular format. This data is stored in a predefined data
model which is not very much flexible for today’s real-world highly growing
applications. Modern applications are more networked, social and interactive
than ever. Applications are storing more and more data and are accessing it at
higher rates.
Relational Database Management System(RDBMS) is not the correct choice
when it comes to handling big data by the virtue of their design since they are
not horizontally scalable. If the database runs on a single server, then it will reach
a scaling limit. NoSQL databases are more scalable and provide superior
performance. MongoDB is such a NoSQL database that scales by adding more and
more servers and increases productivity with its flexible document model.
RDBMS vs MongoDB:
RDBMS has a typical schema design that shows number of tables and
the relationship between these tables whereas MongoDB is document-
oriented. There is no concept of schema or relationship.
Complex transactions are not supported in MongoDB because complex
join operations are not available.
MongoDB allows a highly flexible and scalable document structure. For
example, one data document of a collection in MongoDB can have two
fields whereas the other document in the same collection can have four.
MongoDB is faster as compared to RDBMS due to efficient indexing
and storage techniques.
There are a few terms that are related in both databases. What’s called
Table in RDBMS is called a Collection in MongoDB. Similarly, a Row
is called a Document and a Column is called a Field. MongoDB provides
a default ‘_id’ (if not provided explicitly) which is a 12-byte
hexadecimal number that assures the uniqueness of every document. It is
similar to the Primary key in RDBMS.
Features of MongoDB:
Document Oriented: MongoDB stores the main subject in the minimal
number of documents and not by breaking it up into multiple relational
structures like RDBMS. For example, it stores all the information of a
computer in a single document called Computer and not in distinct
relational structures like CPU, RAM, Hard disk, etc.
Indexing: Without indexing, a database would have to scan every
document of a collection to select those that match the query which
would be inefficient. So, for efficient searching Indexing is a must and
MongoDB uses it to process huge volumes of data in very less time.
Scalability: MongoDB scales horizontally using sharding (partitioning
data across various servers). Data is partitioned into data chunks using
the shard key, and these data chunks are evenly distributed across shards
that reside across many physical servers. Also, new machines can be
added to a running database.
Replication and High Availability: MongoDB increases the data
availability with multiple copies of data on different servers. By
providing redundancy, it protects the database from hardware failures. If
one server goes down, the data can be retrieved easily from other active
servers which also had the data stored on them.
Aggregation: Aggregation operations process data records and return the
computed results. It is similar to the GROUPBY clause in SQL. A few
aggregation expressions are sum, avg, min, max, etc
Where do we use MongoDB?
MongoDB is preferred over RDBMS in the following scenarios:
Big Data: If you have huge amount of data to be stored in tables, think
of MongoDB before RDBMS databases. MongoDB has built-in solution
for partitioning and sharding your database.
Unstable Schema: Adding a new column in RDBMS is hard whereas
MongoDB is schema-less. Adding a new field does not effect old
documents and will be very easy.
Distributed data Since multiple copies of data are stored across
different servers, recovery of data is instant and safe even if there is a
hardware failure.
Language Support by MongoDB:
MongoDB currently provides official driver support for all popular programming
languages like C, C++, Rust, C#, Java, Node.js, Perl, PHP, Python, Ruby, Scala,
Go, and Erlang.
DataTypes in MongoDB
Last Updated : 09 Jun, 2022
In MongoDB, the documents are stores in BSON, which is the binary encoded
format of JSON and using BSON we can make remote procedure calls in
MongoDB. BSON data format supports various data-types. Below are the enlisted
MongoDB data types:
1. String: This is the most commonly used data type in MongoDB to store data,
BSON strings are of UTF-8. So, the drivers for each programming language
convert from the string format of the language to UTF-8 while serializing and de-
serializing BSON. The string must be a valid UTF-8.
Example: In the following example we are storing the name of the student in the
student collection:
Here, the data type of the value of the name field is a string.
2. Integer: In MongoDB, the integer data type is used to store an integer value.
We can store integer data type in two forms 32 -bit signed integer and 64 – bit
signed integer.
Example: In the following example we are storing the age of the student in the
student collection:
3. Double: The double data type is used to store the floating-point values.
Example: In the following example we are storing the marks of the student in the
student collection:
4. Boolean: The boolean data type is used to store either true or false.
Example: In the following example we are storing the final result of the student as
pass or fail in boolean values.
5. Null: The null data type is used to store the null value.
Example: In the following example, the student does not have a mobile number
so the number field contains the value null.
6. Array: The Array is the set of values. It can store the same or different data
types values in it. In MongoDB, the array is created using square brackets([]).
Example: In the following example, we are storing the technical skills of the
student as an array.
7. Object: Object data type stores embedded documents. Embedded documents are
also known as nested documents. Embedded document or nested documents are
those types of documents which contain a document inside another document.
Example: In the following example, we are storing all the information about a
book in an embedded document.
8. Object Id: Whenever we create a new document in the collection MongoDB
automatically creates a unique object id for that document(if the document does
not have it). There is an _id field in MongoDB for each document. The data which
is stored in Id is of hexadecimal format and the length of the id is 12 bytes which
consist:
4-bytes for Timestamp value.
5-bytes for Random values. i.e., 3-bytes for machine Id and 2-bytes for
process Id.
3- bytes for Counter
You can also create your own id field, but make sure that the value of that id field
must be unique.
Example: In the following example, when we insert a new document it creates a
new unique object id for it.
9. Undefined: This data type stores the undefined values.
Example: In the following example the type of the duration of the project is
undefined.
11. Date: Date data type stores date. It is a 64-bit integer which represents the
number of milliseconds. BSON data type generally supports UTC datetime and it
is signed. If the value of the date data type is negative then it represents the dates
before 1970. There are various methods to return date, it can be returned either as a
string or as a date object. Some method for the date:
Date(): It returns the current date in string format.
new Date(): Returns a date object. Uses the ISODate() wrapper.
new ISODate(): It also returns a date object. Uses the ISODate()
wrapper.
Example: In the following example we are using all the above method of the date:
12. Min & Max key: Min key compares the value of the lowest BSON element
and Max key compares the value against the highest BSON element. Both are
internal data types.
Example:
13. Symbol: This data type similar to the string data type. It is generally not
supported by a mongo shell, but if the shell gets a symbol from the database, then
it converts this type into a string type.
Example:
14. Regular Expression: This datatype is used to store regular expressions.
Example: In the following example we are storing the regular expression gfg:
15. JavaScript: This datatype is used to store JavaScript code into the document
without the scope.
Example: In this example, we are using the JavaScript syntax in the shell:
16. JavaScript with Scope: This MongoDB data type store JavaScript data with a
scope. This data type is deprecated in MongoDB 4.4.
Example: In this example, we are using the JavaScript syntax in the shell:
MongoDB Query is a way to get the data from the MongoDB database. MongoDB
queries provide the simplicity in process of fetching data from the database, it’s
similar to SQL queries in SQL Database language. While performing a query
operation, one can also use criteria or conditions which can be used to retrieve
specific data from the database.
MongoDB provides the function names as db.collection_name.find() to operate
query operation on database. In this post, we discussed this function in many ways
using different methods and operators.
Here, we are working with:
Database: geeksforgeeks
Collection: Article
Note: Here “pretty()” query method is using for only better readability of
Document Database.(It’s not necessary)
Field selection
The equality operator($eq) is used to match the documents where the value of the
field is equal to the specified value. In other words, the $eq operator is used to
specify the equality condition.
Syntax:
db.collection_name.find({< key > : {$eq : < value >}})
Example:
db.article.find({author:{$eq:"devil"}}).pretty()
Here, we are going to display the documents that matches the filter
query(i.e., {author : {$eq : “devil”}}) from the article collection.
$exists operator shows all the collection documents if they exist on a given key.
Syntax:
db.collection_name.find({< key > : {$exists : < boolean >}})
Example:
db.article.find({time:{$exists:"true"}}).pretty()
Here, we are going to look all the documents which has the attribute named as time
by passing a filter query that is {time : {$exists : “true”}} in the find() method.
Logical operator query
$and operator comes under the type of MongoDB logical operator which perform
logical AND operation on the array of one or more expressions and select or
retrieve only those documents that match all the given expression in the array.
Syntax :
db.collection_name.find({$and : [{< key > : {$eq : < value1 >}}, {< key > :
{$exists : < boolean >}}]})
Example:
db.article.find({$and:[{level:{$eq:"high"}},{level:{$exists :
"true"}}]}).pretty()
In this query example we are using and operator and given two condition which are
highlighted following
and operator: {$and : [ first condition, second condition]}
first condition(level == “high”): { level : {$eq : “high”}}
second condition: {level : {$exists : “true”}}
Project query
This query returned the result whatever we specify in parameter using 1 and 0
1: It indicated to return the result
0: It indicates to not return the result
These parameters called Projection Parameter.
Syntax:
db. collection_name. find({< key > : < value >}, {<key> : <
Projection_Parameter >})
Example:
db. article. find({author : "devil"},{title : 0}).pretty()
This query example is requesting the data which have the author as named “devil”
but in the record don’t want to show the title attribute by specifying it as projection
parameter 0
Query to find a record having given attribute: { author : “devil”}
Projection Parameter: {title : 0}
Limit Query
In MongoDB, It returns the finding result in sorted order that can be ascending or
descending. The order specifies by given parameter like
1 (Positive One): It indicates the Ascending order of having attribute
value in the record.
-1 (Negative One): It indicates the Descending order of having attribute
value in the record.
Syntax:
db.collection_name.find(). sort({< key > : 1})
Example:
db.article.find({author : "devil"}).sort({example : 1}).pretty()
This Example will show the resultant record which has the example attribute and it
will show in ascending order because here we are passing value 1.
Query to finding record having given attribute: find ({author : “devil”})
Query to sort parameter: sort({example : 1})