Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>

Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

8. Working with MongoDB


CRUD operations on MongoDB

Aim/Objective: The aim of CRUD operations in MongoDB is to enable developers to perform basic
database operations for creating, reading, updating, and deleting data.

Description: CRUD operations (Create, Read, Update, Delete) allow you to interact with MongoDB
and manipulate data within collections.

1. Create (Insert): It involves adding new documents to a collection.


 insertOne(document): Inserts a single document into the collection.
2. Read (Query): It involves retrieving documents from a collection.
 Find(query): Retrieves documents from the collection based on the specified query
criteria.
3. Update: It involves modifying existing documents in a collection.
 updateOne(filter, update): Updates a single document that matches the specified
filter with the provided update.
4. Delete: It involves removing documents from a collection.
 deleteOne(filter): Deletes a single document that matches the specified filter.

Pre-Requisites:

Installation of MongoDB server, MongoDB Shell

Pre-Lab:

1. What is CRUD and how does it relate to MongoDB?


CRUD stands for Create, Read, Update, and Delete – the fundamental operations for managing
data in a database. In MongoDB, a NoSQL database, CRUD operations correspond to creating,
reading, updating, and deleting documents within collections.

2. How do you create a new document in MongoDB?


To create a new document in MongoDB, you use the insertOne() or insertMany() method.
Here's an example of creating a new document using insertOne() in a JavaScript-like syntax:
db.collection("yourCollection").insertOne({
key1: value1,
key2: value2,
// Other key-value pairs
});

3. How do you read or retrieve documents from a collection in MongoDB?


To retrieve documents from a collection in MongoDB, you use the find() method on the
collection object. For example:
const documents = db.collection("yourCollection").find(query).toArray();

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 50 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>
4. How do you update a document in MongoDB?
To update a document in MongoDB, you can use the updateOne() or updateMany() method.
Here's an example using updateOne() to modify a document:
db.collection("yourCollection").updateOne(
{ _id: ObjectId("yourDocumentId") },
{ $set: { keyToUpdate: newValue } }
);

5. How do you delete a document in MongoDB?

db.collection("yourCollection").deleteOne({ _id: ObjectId("yourDocumentId") });

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 51 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

In-Lab:

Exercise 1: Create a new database called “bookstore” and switch to that database. Create a collection
called “books” within the “bookstore” database. Insert three documents (books) into the “books”
collection with the following fields:

 Title: “The Great Gatsby”, Author: “F. Scott Fitzgerald”, Year: 1925
 Title: “To Kill a Mockingbird”, Author: “Harper Lee”, Year: 1960
 Title: “1984”, Author: “George Orwell”, Year: 1949

Retrieve all the documents from the “books” collection and display them.

Exercise 2: Update the document with the title “1984” and change the author to “George Orwell
(pseudonym)”. Delete the document with the title “The Great Gatsby”. Also retrieve all the documents
from the “books” collection and display them to verify that the deletion was successful.

Procedure/Program:

Inlab1

Create a new database and switch to it:


use bookstore

Create a collection called "books" within the "bookstore" database:

db.createCollection("books")

Insert three documents (books) into the "books" collection:


db.books.insertMany([
{
Title: "The Great Gatsby",
Author: "F. Scott Fitzgerald",
Year: 1925
},
{
Title: "To Kill a Mockingbird",
Author: "Harper Lee",
Year: 1960
},
{
Title: "1984",
Author: "George Orwell",
Year: 1949
}
])

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 52 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Retrieve and display all the documents from the "books" collection:

db.books.find().pretty()

Inlab2 program:
db.books.updateOne(
{ Title: "1984" },
{ $set: { Author: "George Orwell (pseudonym)" } }
)

Delete the document with the title "The Great Gatsby":


db.books.deleteOne({ Title: "The Great Gatsby" })

Retrieve and display all the documents from the "books"


collection to verify changes:
db.books.find().pretty()

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 52 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

 Data and Results:

Inlab1 output

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 53 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 54 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Inlab2 output:

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 55 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 56 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Sample VIVA-VOCE Questions (In-Lab):

1. What is CRUD in the context of database operations?

CRUD stands for Create, Read, Update, Delete – the core operations for managing data in a
database.
2. What are the four basic operations in MongoDB for CRUD?
The four basic operations in MongoDB for CRUD are:
Create: Inserting new documents into a collection.
Read: Retrieving documents from a collection based on specified criteria.
Update: Modifying existing documents within a collection.
Delete: Removing documents from a collection based on specified criteria.
3. How do you create a new document in MongoDB?
db.collection("yourCollection").insertOne({ key1: value1, key2: value2,});
4. How do you retrieve/read data from MongoDB?
To retrieve or read data from MongoDB, you use the find() method on a collection object.
This method allows you to specify query criteria to filter the documents
5. How do you update a document in MongoDB?
To update a document in MongoDB, you use the updateOne() or updateMany() method,
providing the filter criteria to locate the document and the update operations to modify its
fields.

Post-Lab:

Question 1: Write queries to

a. Display the first document in the collection – “Borrower” in Library Management System
database.
b. Display all document in the collection – “Borrower” in Library Management System
database.
c. Display only those who borrowed “Let Us C” book.
d. Delete all the borrowers except 4 students named as “Amit”, “Rahul”, “Anjana”, “Afroz”

Procedure/Program:

Display the first document in the "Borrower" collection:

db.Borrower.findOne()

b. Display all documents in the "Borrower" collection

db.Borrower.find().pretty()

Display only those who borrowed the "Let Us C" book:

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 57 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>
db.Borrower.find({ BorrowedBooks: "Let Us C" }).pretty()

d. Delete all the borrowers except 4 students named "Amit", "Rahul", "Anjana", "Afroz":

db.Borrower.deleteMany({ Name: { $nin: ["Amit", "Rahul", "Anjana", "Afroz"] } })

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 58 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 59 of 162
Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

 Output and Results:


Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s)
Evaluator 22SDCS01A/R/P
Remark (if Any): Page 61 of 162

Marks Secured _____ out of 50


Experiment # <TO BE FILLED BY STUDENT> Student ID <TO BE FILLED BY STUDENT>
Date <TO BE FILLED BY STUDENT> Student Name <TO BE FILLED BY STUDENT>

Signature of the Evaluator with Date

Course Title MERNSTACK WEB DEVELOPMENT ACADEMIC YEAR: 2023-24


Course Code(s) 22SDCS01A/R/P Page 56 of 162

You might also like