Lab Manual Mongo DB
Lab Manual Mongo DB
Lab File
B. Tech. - Practical
Experiment Page
S. No. Title of Experiment
Number No.
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.
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.
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:
Step 2: Start the EC2 instance that you have created in Step 1.
Experiment
Code/Procedure
mongod --version
Step 6: If MongoDB is not installed on your virtual machine then at first Install the public
key using the following command.
Step 8: Reload the local package database 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.
Syntax
db.createCollection(„ collection_name‟ );
Example
db.createCollection('Student');
Output:
insertOne()
insertMany()
1. insertOne() Method
Example:
db.myNewCollection1.insertOne( { name:"geeksforgeeks" } )
Example:
db.myNewCollection2.insertMany([{name:"gfg", country:"India"},
{name:"rahul", age:20}])
Output:
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()
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
Here, we insert a document in the “student” collection whose name is “Akshay” and
marks is “500” using insert() method.
db.students.insert({
roll_number: "12345",
viva_questions: [
"What is polymorphism?",
})
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:
Output:
{
"_id": ObjectId("6156d8f013c92ec9f5b8b36f"),
"name": "Alice"
}
In this example, MongoDB automatically generates a unique ObjectId value for the
_id field.
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:
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?