Professional Documents
Culture Documents
Exp-8 Mern
Exp-8 Mern
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.
Pre-Requisites:
Pre-Lab:
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
db.createCollection("books")
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)" } }
)
Inlab1 output
Inlab2 output:
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:
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:
db.Borrower.findOne()
db.Borrower.find().pretty()
d. Delete all the borrowers except 4 students named "Amit", "Rahul", "Anjana", "Afroz":