0% found this document useful (0 votes)
8 views34 pages

NoSQL Databases Overview and MongoDB Guide

The MongoDB lab manual explains core NoSQL concepts and guides students through practical database operations using MongoDB mongo1 . It begins with an introduction to NoSQL types and features, followed by installation and setup of MongoDB and the shell. Students perform CRUD operations, use comparison and logical operators, and work with nested documents and arrays. The manual covers indexing techniques, aggregation pipelines, and advanced operators like $group, $project, and $sort. The final

Uploaded by

mayankyadav5462
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views34 pages

NoSQL Databases Overview and MongoDB Guide

The MongoDB lab manual explains core NoSQL concepts and guides students through practical database operations using MongoDB mongo1 . It begins with an introduction to NoSQL types and features, followed by installation and setup of MongoDB and the shell. Students perform CRUD operations, use comparison and logical operators, and work with nested documents and arrays. The manual covers indexing techniques, aggregation pipelines, and advanced operators like $group, $project, and $sort. The final

Uploaded by

mayankyadav5462
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore

Shri Vaishnav Institute of Information Technology

Experiment No. – 1

Aim: NoSQL Databases

Description:

A NoSQL database (originally meaning non-SQL or non-relational) provides a mechanism


for storage and retrieval of data that is modeled differently from the tabular relations used
in traditional relational databases.
NoSQL databases first appeared in the late 1960s but became popular in the early 21st
century with the rise of big data and real-time web applications. Internet giants like
Google, Facebook, and Amazon adopted NoSQL to handle huge volumes of data effi-
ciently.
Unlike SQL databases, NoSQL systems are often schema-free, horizontally scalable, and
optimized for distributed data storage. They are sometimes referred to as “Not Only
SQL” to emphasize that they may also support SQL-like query languages.

Types of NoSQL Databases:


Document Databases: Document-oriented databases store data as JSON or BSON-
like documents containing key-value pairs with various data types such as strings, arrays,
and objects. They are flexible, ideal for web applications, and commonly represented by
MongoDB.

Key-Value Databases: Key-value databases store data as pairs of unique keys and
their corresponding values. Each key uniquely identifies a value, allowing for quick and
efficient data retrieval. They are best suited for simple lookups, caching, and session
management tasks. Common examples include Redis and DynamoDB.

Wide-Column (Column-Oriented) Databases: Column-oriented databases store


data in tables consisting of rows and dynamic columns, where each row can have a differ-
ent set of columns, providing great flexibility in data storage. They are particularly useful
for handling large-scale data and are commonly used in big data and IoT applications.
Popular examples include Cassandra and HBase.

Graph Databases: Graph databases store data in the form of nodes and edges, where
nodes represent entities and edges represent relationships between them. This structure
makes them ideal for social networks, fraud detection, and recommendation systems.
Common examples include Neo4j and JanusGraph.

1
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Features of NoSQL:
Non-relational: NoSQL databases don’t follow the relational model or use fixed-column
tables. They work with self-contained data structures or BLOBs, don’t require data nor-
malization or object-relational mapping, and avoid complex features like joins, query
planners, and strict ACID compliance.

Schema-free: NoSQL databases are schema-free or have flexible schemas, meaning they
do not require a predefined structure for storing data. This allows for heterogeneous data
structures within the same domain, enabling easy storage and management of different
types of data without strict schema definitions.

Simple API: NoSQL databases provide easy-to-use interfaces for storing and querying
data, often through APIs that support low-level data manipulation and selection methods.
They typically use text-based protocols like HTTP REST with JSON for communication.
Most NoSQL systems do not rely on a standardized query language and are designed as
web-enabled databases that can operate as internet-facing services.

Distributed: NoSQL databases run in a distributed manner with auto-scaling and fail-
over features. They often relax ACID properties for better scalability and use asyn-
chronous replication methods like multi-master or peer-to-peer. Following a shared-
nothing architecture, they ensure high distribution and eventual consistency.

Advantages of NoSQL:
NoSQL databases efficiently handle large, fast, and diverse data. They offer high avail-
ability, fault tolerance, and horizontal scalability with flexible schemas. Supporting all
data types, they integrate well with modern programming languages, work in distributed
and cloud environments, and remove the need for a separate caching layer.

Disadvantages of NoSQL:
NoSQL databases face several challenges, including a lack of standardization across dif-
ferent systems and limited query capabilities compared to SQL. They may experience
consistency issues during simultaneous transactions, and managing keys becomes diffi-
cult as data volumes increase. These databases are not ideal for relational data or complex
joins and often have a steep learning curve for beginners. Additionally, many open-source
NoSQL options provide limited enterprise-level support.

Result:
The result of this experiment is that NoSQL databases provide a flexible, scalable, and
efficient way to store and manage large volumes of diverse data without relying on a fixed
relational schema.
2
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Viva Questions:

1. What is NoSQL?

2. What are the main types of NoSQL databases?

3. What is the difference between SQL and NoSQL?

4. Why use NoSQL databases?

5. What is BASE in NoSQL?

3
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Experiment – 2

Aim: Installation and Setup of MongoDB.

Description :

Open your browser → search “MongoDB Community Server download” → click


the official result.

Figure 1: MongoDB Community Server Download Page

On the download page:

• Version: choose the latest (8.0.13)

• OS: Windows x64

• Package: MSI → click Download

MongoDB Setup Steps:

• Accept License Agreement

• Complete the setup

Download MongoDB Shell:

On the download page, download MongoDB Shell with:

• Version: 2.5.6

• OS: Windows x64

4
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Figure 2: MongoDB Installer Setup Wizard

Figure 3: MongoDB Setup and License Agreement

Figure 4: MongoDB Installation Process and Completion

5
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

• Package: MSI → click Download

Figure 5: MongoDB Shell Download Page

MongoDB Shell Setup:

• Set Destination Folder

• Complete the installation

Figure 6: MongoDB Shell Setup and Destination Folder

6
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

MongoDB Shell Setup Completed Successfully.

Figure 7: MongoDB Shell Running in Command Prompt

Result: Successfully installed and configured MongoDB and MongoDB Shell, verified
their setup by running MongoDB as a service and executing basic commands through
the shell.

Viva Questions

1. What is MongoDB?

2. Which edition of MongoDB do we install for practice?

3. What is the command to start the MongoDB shell?

4. Where does MongoDB store data by default in Windows?

5. How do you check if MongoDB service is running?

7
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Experiment No. – 3

Aim: CRUD Operations


Description
MongoDB provides a set of some basic but most essential operations that help you easily
interact with the MongoDB server. These operations are known as CRUD operations:

• C: Create

• R: Read

• U: Update

• D: Delete

Create Operations

The Create or Insert operations are used to insert or add new documents in a collection.
If a collection does not exist, MongoDB creates one automatically.

Methods:

• [Link]() – Inserts a single document.

• [Link]() – Inserts multiple documents.

Example:

8
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Read Operations

The Read operations are used to retrieve documents from a collection (i.e., query the
data).

Method:

• [Link]() – Retrieves all documents.

Example:

Update Operations

The Update operations are used to modify existing documents in a collection.

Methods:

• [Link]() – Updates a single document matching the criteria.

9
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

• [Link]() – Updates multiple documents matching the crite-


ria.

• [Link]() – Replaces a single document entirely.

Example:

10
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Delete Operations

The Delete operations remove documents from a collection.

Methods:

• [Link]() – Deletes a single document matching the criteria.

• [Link]() – Deletes multiple documents matching the criteria.

Example

Viva Questions

1. What does CRUD stand for in MongoDB?

2. What is a collection in MongoDB?

3. What is a document in MongoDB?

4. How do you insert a single document in MongoDB?

5. How do you insert multiple documents at once?

11
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Experiment No. – 4

Aim: Comparison Operations

Description

MongoDB uses various comparison query operators to compare the values of the docu-
ments. The following table contains the comparison query operators:

Name Description

$eq Matches values that are equal to a specified value.

$gt Matches values that are greater than a specified value.

$gte Matches values that are greater than or equal to a spec-


ified value.

$in Matches any of the values specified in an array.

$lt Matches values that are less than a specified value.

$lte Matches values that are less than or equal to a specified


value.

$ne Matches all values that are not equal to a specified value.

$nin Matches none of the values specified in an array.

Examples

1. Using $eq

12
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

2. Using $gt

3. Using $gte

4. Using $lt

13
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

5. Using $lte

6. Using $ne

14
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

7. Using $in

8. Using $nin

Viva Questions

1. How is $in different from $nin?

2. Can we use multiple comparison operators in a single query?

3. How is $lte different from $lt?

4. Can we use comparison operators with non-numeric fields?

5. Can $eq and $ne be used together in a query?

15
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Experiment No. – 5

Aim: Logical Operations

Description
MongoDB supports logical query operators that are used for filtering data and obtaining
precise results based on given conditions. The following table lists the logical query
operators used in MongoDB:

Operator Description
$and Joins query clauses with a logical AND; returns all documents that
match the conditions of both clauses.
$not Inverts the effect of a query expression and returns documents that do
not match the query expression.
$nor Joins query clauses with a logical NOR; returns all documents that
fail to match both clauses.
$or Joins query clauses with a logical OR; returns all documents that
match the conditions of either clause.

Examples

1. Using $and

16
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

2. Using $or

3. Using $not

17
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

4. Using $nor

Viva Questions

1. What is the difference between $and and $or operators?

2. What is the difference between $not and $nor?

3. Can we use logical operators with comparison operators?

4. Can we combine $and and $or in a single query?

18
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Experiment No. – 6

Aim: MongoDB Nested Documents

Description

Embedded or nested documents are those types of documents that contain a document
inside another document. In other words, when a collection has a document, and this
document contains another document or sub-document, such documents are known as
embedded/nested documents.

Creating Embedded Documents

In MongoDB, you can easily embed a document inside another document. As we know, in
the Mongo shell, documents are represented using curly braces ({}) containing field-value
pairs. Inside these fields, we can embed another document using curly braces, which may
also contain field-value pairs or even another sub-document.

Syntax:
{
field : { field 1 : value1 , field 2 : value2 }
}

Example

19
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Viva Questions

1. What is a nested or embedded document in MongoDB?

2. How can we access fields from a nested document?

3. Which command is used to insert multiple documents in MongoDB?

4. What is the purpose of projection in MongoDB?

5. What are the advantages of using nested documents?

20
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Experiment No. – 7

Aim: MongoDB Arrays

Description

MongoDB supports many data types, and one of them is the Array. Arrays are used to
store lists or multiple values within a single key.

Database: neha Collection: Student

Inserting Student Records with Arrays of Subjects

Query Example

21
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Common Array Operators in MongoDB

Operator Description Example


$push Adds an element to an array. { $push: { subjects:
"Science" } }
$pop Removes the first (-1) or last (1) ele- { $pop: { subjects: 1
ment from an array. } }
$slice Returns a limited number of elements { subjects: { $slice:
from an array. 2 } }
$size Checks the number of elements in an { subjects: { $size:
array. 4 } }
$in Matches any value present in the array. { subjects: { $in:
["Maths"] } }
$all Matches documents that contain all { subjects: { $all:
specified values in the array. ["Maths","English"] } }

Examples of Array Operations

22
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

23
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Viva Questions

1. What is an array in MongoDB?

2. Which operator is used to insert an element into an array?

3. What does the $slice operator do?

4. How can you find the size of an array?

24
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Experiment No. – 8

Aim: MongoDB Indexing

Description

Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan
every document in a collection to find those matching a query, which is inefficient and
requires scanning large data volumes.

Indexes are special data structures that store a small portion of the data set in an easy-
to-traverse form. The index stores the value of specific field(s), ordered by the field
values.

The createIndex() Method

To create an index, use the createIndex() method.

Syntax:
db. COLLECTION_NAME . create Index ({ KEY :1})

Here, KEY is the name of the field on which you want to create the index, and 1 indicates
ascending order. For descending order, use -1.

The dropIndex() Method

This method drops a specific index from the collection.

Syntax:
db. COLLECTION_NAME . drop Index ({ KEY :1})

Here, KEY is the field name of the index you wish to drop.

The dropIndexes() Method

This method deletes multiple (specified) indexes on a collection.

Syntax:
db. COLLECTION_NAME . drop Indexes ()

25
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

The getIndexes() Method

This method returns the description of all the indexes in a collection.

Syntax:
db. COLLECTION_NAME . getIndexes ()

Example

Viva Questions

1. How do you update a specific element of an array in MongoDB?

2. What is indexing in MongoDB?

3. Why do we need indexing in a database?

4. What is the default index created in MongoDB when a collection is created?

5. How do you create an index in MongoDB?

26
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Experiment No. – 9

Aim: MongoDB Aggregation

Description

Aggregation operations process data records and return computed results. They group
values from multiple documents and perform various operations on grouped data to return
a single result.

In SQL, COUNT(*) with GROUP BY is equivalent to MongoDB aggregation.

The aggregate() Method

For aggregation in MongoDB, use the aggregate() method.

Syntax:

db. COLLECTION_NAME . aggregate ( AGGREGATE_OPERATION )

Example:

27
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Aggregation Expressions in MongoDB

Expression Description
$match Filters documents (similar to find() but used inside
aggregation).
$group Groups documents by a field and performs aggregations
(like sum, average, count).
$project Selects specific fields or creates new computed fields.
$sort Sorts documents by a field.
$limit Limits the number of documents returned.
$skip Skips the first N documents.
$count Counts the number of documents in the pipeline.
$sum Used inside $group to calculate totals.
$avg Calculates the average value.
$min Finds the minimum value.
$max Finds the maximum value.

Examples of Aggregation Operations

28
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

29
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

30
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

31
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Viva Questions

1. What is an index intersection?

2. Can a collection have more than one text index? Why or why not?

3. What are partial indexes and when would you use them?

4. Explain TTL (Time-To-Live) indexes.

32
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

Experiment No. – 10

Aim: Map Reduce in MongoDB

Description

As per the MongoDB documentation, Map-Reduce is a data processing paradigm for


condensing large volumes of data into useful aggregated results. MongoDB uses the
mapReduce command for map-reduce operations. It is generally used for processing large
data sets.

MapReduce Command

Syntax:
db. collection . map Reduce (
function () { emit( key , value ); }, // map function
function ( key , values) { return reduce Function ; }, // reduce
‹→ function
{
out: collection ,
query: document ,
sort: document ,
limit: number
}
)

Explanation

The MapReduce function first queries the collection, then maps the result documents to
emit key-value pairs, which are then reduced based on the keys having multiple values.

In the above syntax:

• map – A JavaScript function that maps a value with a key and emits a key-value
pair.

• reduce – A JavaScript function that reduces or groups all documents having the
same key.

• out – Specifies the location of the MapReduce query result.

• query – Specifies the optional selection criteria for documents.

33
Shri Vaishnav Vidyapeeth Vishwavidyalaya, Indore
Shri Vaishnav Institute of Information Technology

• sort – Specifies the optional sort criteria.

• limit – Specifies the optional maximum number of documents to be returned.

Example Implementation

Viva Questions

1. What is MapReduce?

2. What are the roles of the Map and Reduce functions?

3. What is the output of the Map phase?

4. What happens in the Reduce phase?

34

You might also like