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

Lab Manual Mongo DB

The document is a lab file for a Bachelor of Technology in Computer Science Engineering program at SAGE University, Indore, detailing practical experiments related to MongoDB. It includes a table of contents with various experiments covering installation, database creation, and manipulation of data within MongoDB. Each experiment outlines objectives, required tools, procedures, and sample viva questions to assess understanding.

Uploaded by

ravanmorya86
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab Manual Mongo DB

The document is a lab file for a Bachelor of Technology in Computer Science Engineering program at SAGE University, Indore, detailing practical experiments related to MongoDB. It includes a table of contents with various experiments covering installation, database creation, and manipulation of data within MongoDB. Each experiment outlines objectives, required tools, procedures, and sample viva questions to assess understanding.

Uploaded by

ravanmorya86
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

SAGE University, Indore

Institute of Advance Computing Core

Lab File
B. Tech. - Practical

Academic Session: 2024-2025

Program : Bachelor of Technology in CSE,

Course Code : ACTDCDBM001P

Course Name : MongoDB Lab

Submitted by Course Coordinator


Name: Mr .Puneet Gurbani
Name :-
Signature :
Enrollment No:-
Table of Contents

Experiment Page
S. No. Title of Experiment
Number No.

1 1 How to download and install MongoDB on windows.

2 2 Install MongoDB in Cloud: AWS, Google, Azure

3 3 How to Create Database & Collection in MongoDB

4 4 Add MongoDB Array using insert() with Example

MongoDB Primary Key: Example to set _id field with


5 5
ObjectId()

6 6 MongoDB Query Document using find() with Example

7 7 MongoDB order with Sort() & Limit() Query with Examples

8 8 MongoDB Count() & Remove() Functions with Examples

MongoDB Update() Document with Example


9 9

MongoDB Security, Backup & Monitoring


10 10
Experiment Number 01
Experiment Title How to download and install MongoDB on windows.
CO1.To show process of creation secure databases with MongoDB
Course Outcome CO2.To demonstrate manipulation and maintenance of database.
(CO) CO3.To demonstrate creation of model and use data in a No SQL environment
with MongoDB.
Tools/Apparatus MongoDB 4.4
Required Windows 10/11
MongoDB is an open-source document-oriented database. It is categorized under the
Experiment NoSQL(Not only SQL) database because the storage and retrieval of data in MongoDB
Description are not in the form of tables. This is the general introduction to MongoDB now we will
learn how to install MongoDB in Windows.
Steps to Install MongoDB on Windows using MSI
To install MongoDB on Windows, first, download the MongoDB server and then install
the MongoDB shell. The Steps below explain the installation process in detail and provide
the required resources for the smooth download and install MongoDB.

Step 1: Go to the MongoDB Download Center to download the MongoDB Community


Server.

Experiment
Code/Procedure

Here, You can select any version, Windows, and package according to your requirement.
For Windows, we need to choose:
 Version: 7.0.4
 OS: Windows x64
 Package: msi
Step 2: When the download is complete open the msi file and click the next button in the
startup screen:
Step 3: Now accept the End-User License Agreement and click the next button:

Step 4: Now select the complete option to install all the program features. Here, if you
can want to install only selected program features and want to select the location of the
installation, then use the Custom option:
Step 5: Select “Run service as Network Service user” and copy the path of the data
directory. Click Next:

Step 6: Click the Install button to start the MongoDB installation process:
Step 7: After clicking on the install button installation of MongoDB begins:

Step 8: Now click the Finish button to complete the MongoDB installation process:

Step 9: Now we go to the location where MongoDB installed in step 5 in your system and
copy the bin path:

Step 10: Now, to create an environment variable open system properties >> Environment
Variable >> System variable >> path >> Edit Environment variable and paste the copied
link to your environment system and click Ok:

Step 11: After setting the environment variable, we will run the MongoDB server, i.e.
mongod. So, open the command prompt and run the following command:

mongod

Step 12: Now, Open C drive and create a folder named “data” inside this folder create
another folder named “db”. After creating these folders. Again open the command prompt
and run the following command:

mongod
Now, this time the MongoDB server(i.e., mongod) will run successfully.

Run mongo Shell

Step 13: Now we are going to connect our server (mongod) with the mongo shell. So,
keep that mongod window and open a new command prompt window and write mongo.
Now, our mongo shell will successfully connect to the mongod.

Important Point: Please do not close the mongod window if you close this window your
server will stop working and it will not able to connect with the mongo shell.

Now, you are ready to write queries in the mongo Shell.

NA
Input
Output/Result NA
1. What is Mongo shell?
Sample Viva 2. How does MongoDB store data?
Questions with 3. What type of NoSQL database is MongoDB?
Answers (at least 5) 4. List out the important features of MongoDB.
5. What are the data models in MongoDB?
Experiment Number 02
Experiment Title Install MongoDB in Cloud: AWS, Google, Azure
CO1.To show process of creation secure databases with MongoDB
Course Outcome CO2.To demonstrate manipulation and maintenance of database.
(CO) CO3.To demonstrate creation of model and use data in a No SQL environment
with MongoDB.
MongoDB 4.4
1. AWS
accou
nt.
2. EC2
Instan
Tools/Apparatus ce.
Required 3. User
with
privile
ges to
create
Instan
ce.
AWS or Amazon web services is a cloud service platform that provides on-demand
computational services, databases, storage space, and many more services. EC2 or Elastic
Experiment Compute Cloud is a scalable computing service launched on the AWS cloud platform. In
Description simpler words, EC2 is nothing but a virtual computer on which we can perform all our
tasks and we have the authority to configure, launch or even dissipate this virtual
computer
Implementation:

Follow the steps below to install MongoDB on AWS EC2 :

Step 1: Create an AWS Elastic Cloud Compute Instance.

Step 2: Start the EC2 instance that you have created in Step 1.

Experiment
Code/Procedure

Step 3: Connect to your EC2 Instance by clicking on Connect Button.


Step 4: A prompt will pop up after connecting.

Step 5: At first check, if MongoDB is already installed or not.

mongod --version
Step 6: If MongoDB is not installed on your virtual machine then at first Install the public
key using the following command.

wget -qO - https://www.mongodb.org/static/pgp/server-


4.2.asc | sudo apt-key add -

Step 7: Add sources.

echo "deb [ arch=amd64,arm64 ]


https://repo.mongodb.org/apt/ubuntu bionic/mongodb-
org/4.2 multiverse" | sudo tee
/etc/apt/sources.list.d/mongodb-org-4.2.list

Step 8: Reload the local package database using the following command.

sudo apt update

Step 9: Now install the MongoDB packages using the command.

sudo apt install -y mongodb-org

Step 10: Wait for the process to end.


Step 11: We have successfully installed MongoDB on our EC2 instance, to check if
MongoDB is installed or not, verify using the following command.

mongod --version

NA
Input
Output/Result NA
1. Explain the concept of a Document in MongoDB
Sample Viva 2. What is a Collection in MongoDB?
Questions with 3. How does MongoDB ensure data durability?
Answers (at least 5) 4. What is a Replica Set?
5. What are indexes in MongoDB?
Experiment Number 03
Experiment Title How to Create Database & Collection in MongoDB
CO1.To show process of creation secure databases with MongoDB
Course Outcome CO2.To demonstrate manipulation and maintenance of database.
(CO) CO3.To demonstrate creation of model and use data in a No SQL environment
with MongoDB.
Tools/Apparatus MongoDB 4.4
Required Windows 10/11
Experiment In this experiment, we‟ll explore how to create a database, create collections, and insert
Description documents into collections in MongoDB.
Creating a Database and Collection in MongoDB
Step 1: Create a Database In MongoDB
To create a database in MongoDB use the “use” command.
Syntax
use database_name
Example
We have created a MongoDB database named gfgDB, as shown in the image below:

Here, we have created a database named as “gfgDB”. If the database doesn‟t exist,
MongoDB will create the database when you store any data to it.
Note: The "use" command is also used to switch to
another database.
View all the Existing Databases

To view all existing databases in MongoDB use the “show dbs” command.
Experiment
Syntax
Code/Procedure
show dbs
This command returns a list of all the existing MongoDB databases.

To check your current database use the “db” command


Step 2: Create a Collection in MongoDB

To create a collection in MongoDB use the createCollection() method. Collection name is


passed as argument to the method.

Syntax

db.createCollection(„ collection_name‟ );

Example

Creating collection named “Student” in MongoDB

db.createCollection('Student');
Output:

Explanation: In the above example, Student Collection is created using


createCollection() method.

Note: In MongoDB, a collection is also created


directly when we add one or more documents to it.
Step 3: Insert Documents in Collection in MongoDB

We can insert documents in the collection using the two methods:

insertOne()

insertMany()

Let’s understand each of these methods with examples.

1. insertOne() Method

insertOne() method is used to insert only one document in the collection.

Example:

db.myNewCollection1.insertOne( { name:"geeksforgeeks" } )

Here, we create a collection named as “myNewCollection1” by inserting a document that


contains a “name” field with its value in it using insertOne() method.
2. insertMany() method

insertMany() method is used to insert many documents in the collection

Example:

we have created a collection named as myNewCollection2 by inserting two documents


using insertMany() method.

db.myNewCollection2.insertMany([{name:"gfg", country:"India"},

{name:"rahul", age:20}])

Output:

Step 4: View Existing Collections

To view all existing collections in a MongoDB database use the “show collection
command“:

Syntax

show collections

Example

This command returns a list of all the existing collections in the gfgDB database.

Output:
NA
Input
Output/Result NA
1. Explain the concept of a Document in MongoDB
Sample Viva 2. What is a Collection in MongoDB?
Questions with 3. How does MongoDB ensure data durability?
Answers (at least 5) 4. What is a Replica Set?
5. What are indexes in MongoDB?
Experiment Number 04
Experiment Title Add MongoDB Array using insert() with Example
CO1.To show process of creation secure databases with MongoDB
Course Outcome CO2.To demonstrate manipulation and maintenance of database.
(CO) CO3.To demonstrate creation of model and use data in a No SQL environment
with MongoDB.
Tools/Apparatus MongoDB 4.4
Required Windows 10/11
 MongoDB Insert() was used in earlier versions to insert documents into a
collection. It allowed inserting either a single document or multiple documents in
Experiment one go.
Description  However, it has been deprecated since MongoDB version 3.2 in favor of more
specialized methods like insertOne(), insertMany() and bulkWrite().
Examples of MongoDB Insert()

Let‟s see some examples of MongoDB db.Collection.insert() Method to insert new


documents in the MongoDB Collection.

In the following examples, we are working with:

Database: gfg

Collection: student

Document: No document but, we want to insert in the form of the student name and
student marks.

Experiment
Code/Procedure

Example 1: Insert a Document without Specifying an _id Field

Here, we insert a document in the “student” collection whose name is “Akshay” and
marks is “500” using insert() method.

db.student.insert({Name: "Akshay", Marks: 500})


Insert Document with Array: Use the insert() method to add a document to the students
collection. In this example, we will include an array of "Viva Questions".

db.students.insert({

name: "John Doe",

roll_number: "12345",

viva_questions: [

"What is polymorphism?",

"Explain the concept of inheritance.",

"What are the four pillars of OOP?",

"How does a database index work?"

})

NA
Input
Output/Result NA
1. Explain the concept of a Document in MongoDB
Sample Viva 2. What is a Collection in MongoDB?
Questions with 3. How does MongoDB ensure data durability?
Answers (at least 5) 4. What is a Replica Set?
5. What are indexes in MongoDB?
Experiment Number 05
Experiment Title MongoDB Primary Key: Example to set _id field with ObjectId()
CO1.To show process of creation secure databases with MongoDB
Course Outcome CO2.To demonstrate manipulation and maintenance of database.
(CO) CO3.To demonstrate creation of model and use data in a No SQL environment
with MongoDB.
Tools/Apparatus MongoDB 4.4
Required Windows 10/11
Experiment In this experiment, we‟ll explore how to set a primary key in MongoDB by providing
Description detailed explanations and examples to understand and implement this concept effectively.
1. Using the _id Field

MongoDB automatically generates a unique _id value for each document if one is not
provided explicitly. Let‟s see an example:

// Inserting a document without specifying the _id field

db.users.insertOne({ "name": "Alice" });

Output:

{
"_id": ObjectId("6156d8f013c92ec9f5b8b36f"),
"name": "Alice"
}

In this example, MongoDB automatically generates a unique ObjectId value for the
_id field.

Experiment 2. Using Custom _id Field


Code/Procedure
We can specify our own custom _id value for a document. This value should be unique
within the collection to avoid conflicts. Let‟s see an example:

// Inserting a document with a custom _id field


db.users.insertOne({ "_id": 1001, "name": "Bob" });

Output:

{
"_id": 1001,
"name": "Bob"
}

In this example, we explicitly specify the _id field with the value 1001.
3. Using Unique Index

We can create a unique index on a field to apply uniqueness and use it as a primary key.
Let‟s create a unique index on the email field:

// Creating a unique index on the email field


db.users.createIndex({ "email": 1 }, { unique: true
});

Now, when inserting documents, MongoDB will enforce uniqueness on the email field,
effectively making it act as a primary key.
NA
Input
Output/Result NA
1. What is a primary key in MongoDB?
Sample Viva 2. Why is the _id field important in MongoDB?
Questions with 3. Can you change the primary key in MongoDB?
Answers (at least 5) 4. What happens if you try to insert a document with a duplicate _id?
5. How do you create a custom _id field in MongoDB?

You might also like