0% found this document useful (0 votes)
23 views

Unit 4

Uploaded by

mukul.money2003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Unit 4

Uploaded by

mukul.money2003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to NoSQL

Last Updated : 23 Apr, 2024





NoSQL is a type of database management system (DBMS) that is designed to
handle and store large volumes of unstructured and semi-structured data. Unlike
traditional relational databases that use tables with pre-defined schemas to store
data, NoSQL databases use flexible data models that can adapt to changes in data
structures and are capable of scaling horizontally to handle growing amounts of
data.
The term NoSQL originally referred to “non-SQL” or “non-relational” databases,
but the term has since evolved to mean “not only SQL,” as NoSQL databases have
expanded to include a wide range of different database architectures and data
models.
NoSQL databases are generally classified into four main categories:
1. Document databases: These databases store data as semi-structured
documents, such as JSON or XML, and can be queried using document-
oriented query languages.
2. Key-value stores: These databases store data as key-value pairs, and are
optimized for simple and fast read/write operations.
3. Column-family stores: These databases store data as column families,
which are sets of columns that are treated as a single entity. They are
optimized for fast and efficient querying of large amounts of data.
4. Graph databases: These databases store data as nodes and edges, and
are designed to handle complex relationships between data.
NoSQL databases are often used in applications where there is a high volume of
data that needs to be processed and analyzed in real-time, such as social media
analytics, e-commerce, and gaming. They can also be used for other applications,
such as content management systems, document management, and customer
relationship management.
However, NoSQL databases may not be suitable for all applications, as they may
not provide the same level of data consistency and transactional guarantees as
traditional relational databases. It is important to carefully evaluate the specific
needs of an application when choosing a database management system.
NoSQL originally referring to non SQL or non relational is a database that
provides a mechanism for storage and retrieval of data. This data is modeled in
means other than the tabular relations used in relational databases. Such databases
came into existence in the late 1960s, but did not obtain the NoSQL moniker until
a surge of popularity in the early twenty-first century. NoSQL databases are used
in real-time web applications and big data and their use are increasing over time.
 NoSQL systems are also sometimes called Not only SQL to emphasize
the fact that they may support SQL-like query languages. A NoSQL
database includes simplicity of design, simpler horizontal scaling to
clusters of machines,has and finer control over availability. The data
structures used by NoSQL databases are different from those used by
default in relational databases which makes some operations faster in
NoSQL. The suitability of a given NoSQL database depends on the
problem it should solve.
 NoSQL databases, also known as “not only SQL” databases, are a new
type of database management system that has, gained popularity in
recent years. Unlike traditional relational databases, NoSQL databases
are designed to handle large amounts of unstructured or semi-structured
data, and they can accommodate dynamic changes to the data model.
This makes NoSQL databases a good fit for modern web applications,
real-time analytics, and big data processing.
 Data structures used by NoSQL databases are sometimes also viewed as
more flexible than relational database tables. Many NoSQL stores
compromise consistency in favor of availability, speed,, and partition
tolerance. Barriers to the greater adoption of NoSQL stores include the
use of low-level query languages, lack of standardized interfaces, and
huge previous investments in existing relational databases.
 Most NoSQL stores lack true ACID(Atomicity, Consistency, Isolation,
Durability) transactions but a few databases, such as MarkLogic,
Aerospike, FairCom c-treeACE, Google Spanner (though technically a
NewSQL database), Symas LMDB, and OrientDB have made them
central to their designs.
 Most NoSQL databases offer a concept of eventual consistency in which
database changes are propagated to all nodes so queries for data might
not return updated data immediately or might result in reading data that
is not accurate which is a problem known as stale reads. Also,has some
NoSQL systems may exhibit lost writes and other forms of data loss.
Some NoSQL systems provide concepts such as write-ahead logging to
avoid data loss.
 One simple example of a NoSQL database is a document database. In a
document database, data is stored in documents rather than tables. Each
document can contain a different set of fields, making it easy to
accommodate changing data requirements
 For example, “Take, for instance, a database that holds data regarding
employees.”. In a relational database, this information might be stored in
tables, with one table for employee information and another table for
department information. In a document database, each employee would
be stored as a separate document, with all of their information contained
within the document.
 NoSQL databases are a relatively new type of database management
system that hasa gained popularity in recent years due to their scalability
and flexibility. They are designed to handle large amounts of
unstructured or semi-structured data and can handle dynamic changes to
the data model. This makes NoSQL databases a good fit for modern web
applications, real-time analytics, and big data processing.
Key Features of NoSQL:
1. Dynamic schema: NoSQL databases do not have a fixed schema and
can accommodate changing data structures without the need for
migrations or schema alterations.
2. Horizontal scalability: NoSQL databases are designed to scale out by
adding more nodes to a database cluster, making them well-suited for
handling large amounts of data and high levels of traffic.
3. Document-based: Some NoSQL databases, such as MongoDB, use a
document-based data model, where data is stored in a schema-less semi-
structured format, such as JSON or BSON.
4. Key-value-based: Other NoSQL databases, such as Redis, use a key-
value data model, where data is stored as a collection of key-value pairs.
5. Column-based: Some NoSQL databases, such as Cassandra, use a
column-based data model, where data is organized into columns instead
of rows.
6. Distributed and high availability: NoSQL databases are often designed
to be highly available and to automatically handle node failures and data
replication across multiple nodes in a database cluster.
7. Flexibility: NoSQL databases allow developers to store and retrieve data
in a flexible and dynamic manner, with support for multiple data types
and changing data structures.
8. Performance: NoSQL databases are optimized for high performance
and can handle a high volume of reads and writes, making them suitable
for big data and real-time applications.
Advantages of NoSQL: There are many advantages of working with NoSQL
databases such as MongoDB and Cassandra. The main advantages are high
scalability and high availability.
1. High scalability: NoSQL databases use sharding for horizontal scaling.
Partitioning of data and placing it on multiple machines in such a way
that the order of the data is preserved is sharding. Vertical scaling means
adding more resources to the existing machine whereas horizontal
scaling means adding more machines to handle the data. Vertical scaling
is not that easy to implement but horizontal scaling is easy to implement.
Examples of horizontal scaling databases are MongoDB, Cassandra, etc.
NoSQL can handle a huge amount of data because of scalability, as the
data grows NoSQL scalesThe auto itself to handle that data in an
efficient manner.
2. Flexibility: NoSQL databases are designed to handle unstructured or
semi-structured data, which means that they can accommodate dynamic
changes to the data model. This makes NoSQL databases a good fit for
applications that need to handle changing data requirements.
3. High availability: The auto, replication feature in NoSQL databases
makes it highly available because in case of any failure data replicates
itself to the previous consistent state.
4. Scalability: NoSQL databases are highly scalable, which means that
they can handle large amounts of data and traffic with ease. This makes
them a good fit for applications that need to handle large amounts of data
or traffic
5. Performance: NoSQL databases are designed to handle large amounts
of data and traffic, which means that they can offer improved
performance compared to traditional relational databases.
6. Cost-effectiveness: NoSQL databases are often more cost-effective than
traditional relational databases, as they are typically less complex and do
not require expensive hardware or software.
7. Agility: Ideal for agile development.
Disadvantages of NoSQL: NoSQL has the following disadvantages.
1. Lack of standardization: There are many different types of NoSQL
databases, each with its own unique strengths and weaknesses. This lack
of standardization can make it difficult to choose the right database for a
specific application
2. Lack of ACID compliance: NoSQL databases are not fully ACID-
compliant, which means that they do not guarantee the consistency,
integrity, and durability of data. This can be a drawback for applications
that require strong data consistency guarantees.
3. Narrow focus: NoSQL databases have a very narrow focus as it is
mainly designed for storage but it provides very little functionality.
Relational databases are a better choice in the field of Transaction
Management than NoSQL.
4. Open-source: NoSQL is an databaseopen-source database. There is no
reliable standard for NoSQL yet. In other words, two database systems
are likely to be unequal.
5. Lack of support for complex queries: NoSQL databases are not
designed to handle complex queries, which means that they are not a
good fit for applications that require complex data analysis or reporting.
6. Lack of maturity: NoSQL databases are relatively new and lack the
maturity of traditional relational databases. This can make them less
reliable and less secure than traditional databases.
7. Management challenge: The purpose of big data tools is to make the
management of a large amount of data as simple as possible. But it is not
so easy. Data management in NoSQL is much more complex than in a
relational database. NoSQL, in particular, has a reputation for being
challenging to install and even more hectic to manage on a daily basis.
8. GUI is not available: GUI mode tools to access the database are not
flexibly available in the market.
9. Backup: Backup is a great weak point for some NoSQL databases like
MongoDB. MongoDB has no approach for the backup of data in a
consistent manner.
10.Large document size: Some database systems like MongoDB and
CouchDB store data in JSON format. This means that documents are
quite large (BigData, network bandwidth, speed), and having descriptive
key names actually hurts since they increase the document size.
Types of NoSQL database: Types of NoSQL databases and the name of the
database system that falls in that category are:
1. Graph Databases: Examples – Amazon Neptune, Neo4j
2. Key value store: Examples – Memcached, Redis, Coherence
3. Column: Examples – Hbase, Big Table, Accumulo
4. Document-based: Examples – MongoDB, CouchDB, Cloudant
When should NoSQL be used:
1. When a huge amount of data needs to be stored and retrieved.
2. The relationship between the data you store is not that important
3. The data changes over time and is not structured.
4. Support of Constraints and Joins is not required at the database level
5. The data is growing continuously and you need to scale the database
regularly to handle the data.
In conclusion, NoSQL databases offer several benefits over traditional relational
databases, such as scalability, flexibility, and cost-effectiveness. However, they
also have several drawbacks, such as a lack of standardization, lack of ACID
compliance, and lack of support for complex queries. When choosing a database
for a specific application, it is important to weigh the benefits and drawbacks
carefully to determine the best fit.

NoSQL business drivers :


NoSQL databases have gained significant traction in the big data landscape due to
several key business drivers. These drivers are motivated by the need to handle large
volumes of diverse and dynamic data efficiently and effectively. Here are the primary
business drivers for adopting NoSQL in big data environments:

### 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.

### 4. **Cost Efficiency**


- **Commodity Hardware:** NoSQL databases can run on clusters of inexpensive,
commodity servers, reducing infrastructure costs.
- **Open Source Options:** Many NoSQL solutions are open-source, which lowers
licensing costs and offers greater control over customization and optimization.

### 5. **Handling Big Data Characteristics**


- **Volume:** NoSQL databases efficiently manage large-scale data storage and
processing requirements.
- **Variety:** They handle diverse data types, making it easier to integrate various
data sources.
- **Velocity:** Capable of processing high-velocity data streams in real-time,
essential for applications like social media analytics and financial transactions.
- **Veracity:** Advanced NoSQL databases incorporate mechanisms to ensure data
accuracy and reliability, which is critical for decision-making processes.
### 6. **High Availability and Reliability**
- **Distributed Architecture:** NoSQL databases are designed to operate in
distributed environments, providing high availability and fault tolerance.
- **Replication and Sharding:** These features ensure data is replicated across
multiple nodes and distributed evenly, minimizing the risk of data loss and downtime.

### 7. **Support for Modern Applications**


- **Cloud Integration:** NoSQL databases are often designed with cloud
environments in mind, providing seamless integration with cloud-based services and
infrastructure.
- **Microservices and DevOps:** They are well-suited for microservices
architectures and DevOps practices, enabling agile development and deployment
cycles.

### 8. **Enhanced Analytics and Insights**


- **Advanced Query Capabilities:** Many NoSQL databases offer powerful
querying capabilities, enabling complex analytics and real-time insights.
- **Integration with Big Data Tools:** They integrate well with big data processing
frameworks like Apache Hadoop, Apache Spark, and real-time data pipelines.

By addressing these business drivers, NoSQL databases empower organizations to


effectively manage and analyze large and complex datasets, providing a robust
foundation for modern data-driven applications and decision-making processes.

NoSQL Data Architecture Patterns


Last Updated : 21 Sep, 2021



Architecture Pattern is a logical way of categorizing data that will be stored on
the Database. NoSQL is a type of database which helps to perform operations on
big data and store it in a valid format. It is widely used because of its flexibility
and a wide variety of services.
Architecture Patterns of NoSQL:
The data is stored in NoSQL in any of the following four data architecture
patterns.
1. Key-Value Store Database
2. Column Store Database
3. Document Database
4. Graph Database
These are explained as following below.
1. Key-Value Store Database:
This model is one of the most basic models of NoSQL databases. As the name
suggests, the data is stored in form of Key-Value Pairs. The key is usually a
sequence of strings, integers or characters but can also be a more advanced data
type. The value is typically linked or co-related to the key. The key-value pair
storage databases generally store data as a hash table where each key is unique.
The value can be of any type (JSON, BLOB(Binary Large Object), strings, etc).
This type of pattern is usually used in shopping websites or e-commerce
applications.
Advantages:
 Can handle large amounts of data and heavy load,
 Easy retrieval of data by keys.
Limitations:
 Complex queries may attempt to involve multiple key-value pairs which
may delay performance.
 Data can be involving many-to-many relationships which may collide.
Examples:
 DynamoDB
 Berkeley DB

2. Column Store Database:


Rather than storing data in relational tuples, the data is stored in individual cells
which are further grouped into columns. Column-oriented databases work only on
columns. They store large amounts of data into columns together. Format and titles
of the columns can diverge from one row to other. Every column is treated
separately. But still, each individual column may contain multiple other columns
like traditional databases.
Basically, columns are mode of storage in this type.
Advantages:
 Data is readily available
 Queries like SUM, AVERAGE, COUNT can be easily performed on
columns.
Examples:
 HBase
 Bigtable by Google
 Cassandra

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

Figure – Document Store Model in form of JSON documents


4. Graph Databases:
Clearly, this architecture pattern deals with the storage and management of data in
graphs. Graphs are basically structures that depict connections between two or
more objects in some data. The objects or entities are called as nodes and are
joined together by relationships called Edges. Each edge has a unique identifier.
Each node serves as a point of contact for the graph. This pattern is very
commonly used in social networks where there are a large number of entities and
each entity has one or many characteristics which are connected by edges. The
relational database pattern has tables that are loosely connected, whereas graphs
are often very strong and rigid in nature.
Advantages:
 Fastest traversal because of connections.
 Spatial data can be easily handled.
Limitations:
Wrong connections may lead to infinite loops.
Examples:
 Neo4J
 FlockDB( Used by Twitter)

Figure – Graph model format of NoSQL Databases

variations of NOSQL architectural patterns using NoSQL to


manage big data:
NoSQL databases come in various architectural patterns, each optimized for specific
use cases and types of data. These variations enable organizations to manage and
analyze big data effectively by selecting the most appropriate NoSQL database for
their needs. Here are the primary NoSQL architectural patterns:

### 1. **Key-Value Stores**


- **Description:** Key-value stores are the simplest type of NoSQL databases. They
store data as a collection of key-value pairs, where each key is unique.
- **Use Cases:** Session management, caching, user preferences, and real-time
analytics.
- **Examples:** Redis, Amazon DynamoDB, Riak.
- **Advantages:** High performance, simplicity, and scalability.

### 2. **Document Stores**


- **Description:** Document stores manage data in the form of documents,
typically JSON, BSON, or XML. Each document contains key-value pairs but can also
embed complex data structures.
- **Use Cases:** Content management systems, e-commerce platforms, and
applications requiring flexible, semi-structured data.
- **Examples:** MongoDB, CouchDB, Amazon DocumentDB.
- **Advantages:** Flexible schema, hierarchical data storage, and efficient
querying.

### 3. **Column-Family Stores (Wide-Column Stores)**


- **Description:** Column-family stores organize data into rows and columns, but
unlike traditional relational databases, columns are grouped into families, allowing
for sparse data storage.
- **Use Cases:** Time-series data, large-scale data analytics, and data
warehousing.
- **Examples:** Apache Cassandra, HBase, ScyllaDB.
- **Advantages:** High write and read performance, scalability, and efficient
handling of wide datasets.

### 4. **Graph Databases**


- **Description:** Graph databases use graph structures with nodes, edges, and
properties to represent and store data. They are designed to handle data with
complex relationships and interconnected entities.
- **Use Cases:** Social networks, recommendation engines, fraud detection, and
network analysis.
- **Examples:** Neo4j, Amazon Neptune, OrientDB.
- **Advantages:** Efficient traversal and querying of relationships, flexibility, and
intuitive data modeling for interconnected data.

### 5. **Time-Series Databases**


- **Description:** Time-series databases are optimized for storing and querying
time-stamped or time-series data. They are designed to handle large volumes of
sequentially ordered data points.
- **Use Cases:** IoT data, financial market data, monitoring and logging, and
performance metrics.
- **Examples:** InfluxDB, TimescaleDB, OpenTSDB.
- **Advantages:** High performance for time-series data, specialized querying, and
efficient storage.

### 6. **Search Engines**


- **Description:** Search engines are specialized NoSQL databases designed for
indexing and searching large volumes of text data. They support complex search
queries and text analytics.
- **Use Cases:** Full-text search, log analysis, and big data exploration.
- **Examples:** Elasticsearch, Apache Solr.
- **Advantages:** Powerful search capabilities, real-time indexing, and scalability.

### 7. **Multimodel Databases**


- **Description:** Multimodel databases support multiple data models (e.g.,
document, graph, key-value) within a single database engine, allowing users to
leverage different models based on their needs.
- **Use Cases:** Complex applications requiring different types of data storage and
retrieval patterns.
- **Examples:** ArangoDB, MarkLogic, OrientDB.
- **Advantages:** Flexibility, reduced complexity, and unified data management.
### 8. **Object-Oriented Databases**
- **Description:** Object-oriented databases store data in the form of objects,
similar to object-oriented programming. They are designed to work well with
applications that use complex data structures and relationships.
- **Use Cases:** CAD/CAM, multimedia applications, and applications with
complex data interactions.
- **Examples:** db4o, ObjectDB.
- **Advantages:** Seamless integration with object-oriented programming
languages, complex data handling, and natural data modeling.

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.

10. Binary Data: This datatype is used to store binary data.


Example: In the following example the value stored in the binaryValue field is of
binary type.

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:

17. Timestamp: In MongoDB, this data type is used to store a timestamp. It is


useful when we modify our data to keep a record and the value of this data type is
64-bit. The value of the timestamp data type is always unique.
Example:
18. Decimal: This MongoDB data type store 128-bit decimal-based floating-point
value. This data type was introduced in MongoDB version 3.4
Example:

What is a MongoDB Query?


Last Updated : 08 Feb, 2021



MongoDB, the most popular open-source document-oriented database is a NoSQL
type of database. NoSQL database stands for Non-Structured Query Database.
MongoDB stores the data in the form of the structure(field:value pair) rather than
tabular form. It stores data in BSON (Binary JSON) format just like JSON format.
A simple example of a MongoDB database collection.
{
“_id” : ObjectId(“6009585d35cce6b7b8f087f1”),
“title” : “Math”,
“author” : “Aditya”,
“level” : “basic”,
“length” : 230,
“example” : 11
}

What is MongoDB Query?

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 find() method displays the database collection in Non-Structured


form({<Key> : <value>}) including auto-created <key> ” id ” by MongoDB and
collection data inserted by user or admin.
Syntax:
db.collection_name.find()
Example:
db.article.find()
This method is used to display all the documents present in the article collection.
Finding a single document

In MongoDB, we can find a single document using findOne() method, This


method returns the first document that matches the given filter query expression.
Syntax:
db.collection_name.findOne ()
Example:
db.article.findOne()
Here, we are going to display the first document of the article collection.

Displaying documents in a formatted way


In MongoDB, we can display documents of the specified collection in well-
formatted way using pretty() method.
Syntax:
db.collection_name.find().pretty()
Example:
db.article.find().pretty()
Here, we are going to display the documents of the article collection in a well-
formatted way using pretty() method.

Equal filter query

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.

Greater than filter query


To get the specific numeric data using conditions like greater than equal or less
than equal use the $gte or $lte operator in the find() method.
Syntax:
db.collection_name.find({< key > : {$gte : < value >}})
or
db.collection_name.find({< key > : {$lte : < value >}})
Example:
db.article.find({length:{$gte:510}}).pretty()
Here, we are querying to get documented data which has the length attribute value
greater than 510. So, we pass a filter query that is {length : {$gte : 510}} in the
find() method.
Check the existence filter query

$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

This query method specifies a maximum number of documents for a cursor to


return.
Syntax :
db.collection_name.find({< key > : < value >}).limit(< Integer_value >)
Example:
db. article. find({author : “devil” }). limit(2) . pretty()
This query method is simply the extension of the find method only provide a result
at a maximum limited number(here is 2)
 Query to find record having given attribute: find({author : “devil” })
 Query to limit result: limit( 2)
Output :
Sort the fields

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})

You might also like