You are on page 1of 16

NOSQL DATABASES

Course Outcomes

 Define, compare and use the four types of NoSQL Databases (Document-oriented, Key Value
Pairs, Column-oriented and Graph).
 Demonstrate an understanding of the detailed architecture, define objects, load data, query
data and performance tune Column-oriented NoSQL databases.
 Explain the detailed architecture, define objects, load data, query data and performance
tune Document-oriented NoSQL databases.
Syllabus
UNIT – I
Define, compare and use the four types of NoSQL Databases (Document-oriented, Key Value
Pairs, Column-oriented and Graph).
Demonstrate an understanding of the detailed architecture, define objects, load data, query data and
performance tune Column-oriented NoSQL databases.
Explain the detailed architecture, define objects, load data, query data and performance tune
Document- oriented NoSQL databases.
UNIT – II
Comparison of relational databases to new NoSQL stores, MongoDB, Cassandra, HBASE, Neo4j
use and deployment, Application, RDBMS approach, Challenges NoSQL approach, Key-Value and
Document Data Models, Column-Family Stores, Aggregate-Oriented Databases.
UNIT – III
Replication and sharding, MapReduce on databases. Distribution Models, Single Server,
Sharding, Master- Slave Replication, Peer-to-Peer Replication, Combining Sharding and Replication.
NoSQL Key/Value databases using MongoDB, Document Databases, What Is a Document Database?
Features, Consistency, Transactions, Availability, Query Features, Scaling, Suitable Use Cases, Event
Logging, Content Management Systems, Blogging Platforms, Web Analytics or Real-Time Analytics, E-
Commerce
Applications, When Not to Use, Complex Transactions Spanning Different Operations, Queries against
Varying Aggregate Structure.

UNIT – IV
Column- oriented NoSQL databases using Apache HBASE, Column-oriented NoSQL databases
using Apache Cassandra, Architecture of HBASE, What Is a Column-Family Data Store? Features,
Consistency, Transactions, Availability, Query Features, Scaling, Suitable Use Cases, Event Logging,
Content Management Systems, Blogging Platforms, Counters, Expiring Usage, When Not to Use.

UNIT – V
NoSQL Key/Value databases using Riak, Key-Value Databases, What Is a Key-Value Store,
Key-Value Store Features, Consistency, Transactions, Query Features, Structure of Data, Scaling,
Suitable Use Cases, Storing Session Information, User Profiles, Preferences, Shopping Cart Data, When
Not to Use, Relationships among Data, Multioperation Transactions, Query by Data, Operations by Sets.

Text Books:
1.NoSQL Distilled: A Brief Guide to the Emerging World of Polyglot Persistence Sadalage, P. & Fowler
Pearson Education

1
Unit-1 NOSQL Databases

Unit-1

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

Dept. of MCA PAGE 2


Unit-1 NOSQL Databases

 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 scalessemi-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.

Dept. of MCA PAGE 3


Unit-1 NOSQL Databases

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

Dept. of MCA PAGE 4


Unit-1 NOSQL Databases

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.
Dept. of MCA PAGE 5
Unit-1 NOSQL Databases

Types of NoSQL Databases


A database is a collection of structured data or information which is stored in a computer system
and can be accessed easily. A database is usually managed by a Database Management System
(DBMS). NoSQL is a non-relational database that is used to store the data in the nontabular form.
NoSQL stands for Not only SQL. The main types are documents, key-value, wide-column, and graphs.
Types of NoSQL Database:
 Document-based databases
 Key-value stores
 Column-oriented databases
 Graph-based databases
Document-Based Database:
The document-based database is a nonrelational database. Instead of storing the data in rows
and columns (tables), it uses the documents to store the data in the database. A document database
stores data in JSON, BSON, or XML documents.

Documents can be stored and retrieved in a form that is much closer to the data objects used in
applications which means less translation is required to use these data in the applications. In the
Document database, the particular elements can be accessed by using the index value that is assigned
for faster querying.

Collections are the group of documents that store documents that have similar contents. Not all
the documents are in any collection as they require a similar schema because document databases have
a flexible schema.
Key features of documents database:
 Flexible schema: Documents in the database has a flexible schema. It means the documents
in the database need not be the same schema.
 Faster creation and maintenance: the creation of documents is easy and minimal
maintenance is required once we create the document.
 No foreign keys: There is no dynamic relationship between two documents so documents can
be independent of one another. So, there is no requirement for a foreign key in a document
database.
 Open formats: To build a document we use XML, JSON, and others.
Key-Value Stores:
A key-value store is a nonrelational database. The simplest form of a NoSQL database is a
key-value store. Every data element in the database is stored in key-value pairs. The data can be
retrieved by using a unique key allotted to each element in the database. The values can be simple
data types like strings and numbers or complex objects.

A key-value store is like a relational database with only two columns which is the key and the value.

Dept. of MCA PAGE 6


Unit-1 NOSQL Databases

Key features of the key-value store:

 Simplicity.
 Scalability.
 Speed.
Column Oriented Databases:
A column-oriented database is a non-relational database that stores the data in columns instead
of rows. That means when we want to run analytics on a small number of columns, you can read those
columns directly without consuming memory with the unwanted data.

Columnar databases are designed to read data more efficiently and retrieve the data with greater
speed. A columnar database is used to store a large amount of data. Key features of columnar
oriented database:

 Scalability.
 Compression.
 Very responsive.
Graph-Based databases:
Graph-based databases focus on the relationship between the elements. It stores the data in the
form of nodes in the database. The connections between the nodes are called links or relationships.
Key features of graph database:
 In a graph-based database, it is easy to identify the relationship between the data by using
the links.
 The Query’s output is real-time results.
 The speed depends upon the number of relationships among the database elements.
 Updating data is also easy, as adding a new node or edge to a graph database is a
straightforward task that does not require significant schema changes.
 NoSQL Data Architecture Patterns
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.

Dept. of MCA PAGE 7


Unit-1 NOSQL Databases

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
Dept. of MCA PAGE 8
Unit-1 NOSQL Databases

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

Dept. of MCA PAGE 9


Unit-1 NOSQL Databases

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


 NoSQL database types explained: Column-oriented databases
The row key is the first column in each column family, and it serves as an identifier of a row.
Furthermore, each column after that has a column key (name). It identifies columns within rows and thus
enables the querying of the columns. The value and the timestamp come after the column key, leaving a
trace of when the data was entered or modified.
The number of columns pertaining to each row, or their name, can vary. In other words, not every column
of a column family, and thus a database, has the same number of rows. In fact, even though they might
share their name, each column is contained within one row and does not run across all rows.

Dept. of MCA PAGE 10


Unit-1 NOSQL Databases

Column-oriented databases use vertical organization as opposed to the horizontal layout of row
databases.
Those who have encountered relational databases know that each column of a relational
database has the same number of rows, but it happens that some of the fields have a null value, or they
appear to be empty. With wide-column databases, rather than being empty, these rows simply do not
exist for a particular column.
Column family types
 Standard column family. This column family type is similar to a table; it contains a key-value
pair where the key is the row key, and the values are stored in columns using their names as their
identifiers.
 Super column family. A super column represents an array of columns. Each super column has a
name and a value mapping the super column out to several different columns. Related super
columns are joined under a single row into super column families. Compared to a relational
database, this is like a view of several different tables within a database. Imagine you had the
view of the columns and values available for a single row -- that is a single identifier across
many different tables -- and were able to store them all in one place: That is the super column
family.
Advantages of column-oriented databases
 Scalability. This is a major advantage and one of the main reasons this type of database is
used to store big data. With the ability to be spread over hundreds of different machines
depending on the scale of the database, it supports massively parallel processing. This means it
can employ many processors to work on the same set of computations simultaneously.
 Compression. Not only are they infinitely scalable, but they are also good at compressing data
and thus saving storage.
 Very responsive. The load time is minimal, and queries are performed fast, which is expected
given that they are designed to hold big data and be practical for analytics.

Dept. of MCA PAGE 11


Unit-1 NOSQL Databases

Disadvantages of column-oriented databases


 Online transactional processing. These databases are not very efficient with online
transactional processing as much as they are for online analytical processing. This means they
are not very good with updating transactions but are designed to analyze them. This is why they
can be found holding data required for business analysis with a relational database storing
data in the back end.
 Incremental data loading. As mentioned above, typically column-oriented databases are used
for analysis and are quick to retrieve data, even when processing complex queries, as it is kept
close together in columns. While incremental data loads are not impossible, columnar databases
do not perform them in the most efficient way. The columns first need to be scanned to identify
the right rows and scanned further to locate the modified data which requires overwriting.
 Row-specific queries. Like the potential downfalls mentioned above, it all boils down to the
same issue, which is using the right type of database for the right purposes. With row-specific
queries, you are introducing an extra step of scanning the columns to identify the rows and then
locating the data to retrieve. It takes more time to get to individual records scattered in multiple
columns, rather than accessing grouped records in a single column. Frequent row-specific queries
might cause performance issues by slowing down a column-oriented database, which is
particularly designed to help you get to required pieces of information quickly, thus defeating
its purpose.
NoSQL databases are mostly designed to fit specific purposes and are not expected to work as
a general type of storage. Wide-column databases are column-oriented rather than row-oriented and
are intended to store and query big data. There are many different databases available within the
type and it is worth exploring their features while on a hunt for the most suitable data storage solution.
 Document Databases in NoSQL
Document Data Model:
A Document Data Model is a lot different than other data models because it stores data in
JSON, BSON, or XML documents. in this data model, we can move documents under one document and
apart from this, any particular elements can be indexed to run queries faster. Often documents are
stored and retrieved in such a way that it becomes close to the data objects which are used in many
applications which means very less translations are required to use data in applications. JSON is a
native language that is often used to store and query data too.
So in the document data model, each document has a key-value pair below is an example for the same.
{
"Name" : "Yashodhra",
"Address" : "Near Patel Nagar",
"Email" : "yahoo123@yahoo.com",
"Contact" : "12345"
}
Dept. of MCA PAGE 12
Unit-1 NOSQL Databases

Working of Document Data Model:


This is a data model which works as a semi-structured data model in which the records and data
associated with them are stored in a single document which means this data model is not completely
unstructured. The main thing is that data here is stored in a document.
Features:
 Document Type Model: As we all know data is stored in documents rather than tables or graphs,
so it becomes easy to map things in many programming languages.
 Flexible Schema: Overall schema is very much flexible to support this statement one must know
that not all documents in a collection need to have the same fields.
 Distributed and Resilient: Document data models are very much dispersed which is the reason
behind horizontal scaling and distribution of data.
 Manageable Query Language: These data models are the ones in which query language
allows the developers to perform CRUD (Create Read Update Destroy) operations on the data
model.
Examples of Document Data Models :
 Amazon DocumentDB
 MongoDB
 Cosmos DB
 ArangoDB
 Couchbase Server
 CouchDB
Advantages:
 Schema-less: These are very good in retaining existing data at massive volumes because there
are absolutely no restrictions in the format and the structure of data storage.
 Faster creation of document and maintenance: It is very simple to create a document and
apart from this maintenance requires is almost nothing.
 Open formats: It has a very simple build process that uses XML, JSON, and its other forms.
 Built-in versioning: It has built-in versioning which means as the documents grow in size there
might be a chance they can grow in complexity. Versioning decreases conflicts.
Disadvantages:
 Weak Atomicity: It lacks in supporting multi-document ACID transactions. A change in the
document data model involving two collections will require us to run two separate queries i.e.
one for each collection. This is where it breaks atomicity requirements.
 Consistency Check Limitations: One can search the collections and documents that are not
connected to an author collection but doing this might create a problem in the performance of
database performance.

Dept. of MCA PAGE 13


Unit-1 NOSQL Databases

 Security: Nowadays many web applications lack security which in turn results in the leakage of
sensitive data. So it becomes a point of concern, one must pay attention to web app
vulnerabilities.
Applications of Document Data Model:
 Content Management: These data models are very much used in creating various video
streaming platforms, blogs, and similar services Because each is stored as a single document
and the database here is much easier to maintain as the service evolves over time.
 Book Database: These are very much useful in making book databases because as we know this
data model lets us nest.
 Catalog: When it comes to storing and reading catalog files these data models are very much
used because it has a fast reading ability if incase Catalogs have thousands of attributes stored.
 Analytics Platform: These data models are very much used in the Analytics Platform.
 Querying in NoSQL
NoSQL is termed non-SQL. We won’t use SQL to write queries in No SQL. It is not relational but it
gives us an organized way of storing data. The data is stored in the form of documents rather than
tabular form. The best example for NoSql is Mongo DB. In SQL we will use the term key-value pairs but
in Mongo DB we will use field-value pairs. Documents are stored and the group of documents is called
“Collection”. The document will be in JSON format. The data is called a “Document” and the collection
of documents is called a “Collection”.
Steps to query in Mongo DB:
 Install the MongoDB installer.
 Specify a custom directory for mongo.
 After installation runs the Mongo DB daemon.
 Connect to Mongo shell.
 You can start to code.
Querying in NoSQL:
Suppose we want to get specific results on the transport database.
{
"Brand":"Benz"
"Max_Speed":250
"Color":"Green"
}
1. To display the vehicles which have a speed greater than 100.
Query:
>db.transport.find({Max_speed:
{$gt:100}}).pretty()
Output:
{
Dept. of MCA PAGE 14
Unit-1 NOSQL Databases

"Brand":"Benz"
"Max_Speed":250
"Color":"Green"
}
2. To display the vehicles which have a speed equal to 250.
Query:
>db.transport.find({Max_speed:
{$eq:250}}}.pretty()
Output:
{
"Brand":"Benz"
"Max_Speed":250
"Color":"Green"
}
$eq – This operator is used to check 2 values and returns the data which is equal to the specified value.
So like this, we have $gte ( greater than or equal to ), $lte ( lesser than or equal to ), $lt( less than ),
$ne( Not equal ) in NoSQL.
3. To display the vehicles which have a speed lesser than 500 and brand as Benz.
To write this query we need (and) operator.
Query:
>db.transport.find({$and:
[{Max_speed:{$lt:500}},{Brand:
{$eq:"Benz"}}}.pretty()}]})
So the output will be the data where the brand is Benz and also the max_speed will be less
than 500.
Output:
{
"Brand":"Benz"
"Max_Speed":250
"Color":"Green"
}
Insertion in NoSql
In MongoDB, there is no command to create a database. When we save the file the DBMS
automatically creates a database. Here we use the transport database.
> use transport
So let’s insert a data of brand as Hyundai, max_speed as 100, and color as blue.
Query:
db.transport.insert({"Brand":"Hyundai"},
Dept. of MCA PAGE 15
Unit-1 NOSQL Databases

{"Max_speed":100},{"Color":"blue"})
Output:
WriteResult({ "nInserted": 2})
Selection in NoSql
We have inserted the data and we need to see the whole collection so the command is:
Query:
>db.transport.find()
Output:
{
"Brand":"Benz"
"Max_Speed":250
"Color":"Green"
}
{
"Brand":"Hyundai"
"Max_Speed":100
"Color":"Blue"
}

Dept. of MCA PAGE 16

You might also like