Mongo DB Installation reference: https://www.youtube.com/watch?
v=KYIOJrE3zjk
Running CRUD Operations in MongoDB Compass
MongoDB Compass is a GUI tool that allows you to interact with MongoDB without using the
command-line shell. You can create, read, update, and delete (CRUD) documents visually.
I. Connect to MongoDB
1. Open MongoDB Compass.
2. Click "New Connection" and enter the connection string
mongodb://localhost:27017
3.Click "Connect" to establish a connection.
II.Create (Insert Data)
Method 1: Manually Insert a Document
1. Select or create a Database (e.g., myDatabase).
2. Select or create a Collection (e.g., users).
3. Click "Insert Document".
4. Enter JSON-like data:
"name": "John",
"age": 25,
"city": "New York"
5.Click "Insert" to save.
Method 2: Insert Multiple Documents
1. Click "Insert Document" → "Insert Multiple Documents".
2.Enter Json like data
{ "name": "Alice", "age": 30, "city": "Los Angeles" },
{ "name": "Bob", "age": 22, "city": "Chicago" }
III. Read (Retrieve Data)
Find All Documents
1. Open the Collection (users).
2. Click "Find" (or refresh the collection).
3. View documents in Table or JSON format.
Find Documents with Conditions
In the Filter box, enter a query (e.g., find users older than 25):
{ "age": { "$gt": 25 } }
IV. Update (Modify Data)
Method 1: Update a Single Document
1. Find a document, click the Edit (pencil) icon, and modify fields.
2. Click "Update".
Method 2: Update Using a Query
1. Click "Update" → "Update One" or "Update Many".
2. In Filter, specify which documents to update
{ "name": "John" }
3. In Update, specify the changes
{ "$set": { "age": 26 } }
……………………………………………………………………………………………………………………………………………….
Example 2: Example: Employee Records
1. Creating (Inserting) Documents
Inserting a Single Employee Document
1. Open MongoDB Compass and navigate to your database (e.g., companyDB).
2. Select or create a Collection (e.g., employees).
3. Click "Insert Document" and add the following:
{
"name": "Emma Smith",
"age": 28,
"position": "Software Engineer",
"department": "IT",
"salary": 75000,
"joining_date": "2023-05-10"
Inserting Multiple Employee Documents
1. Click "Insert Document" → "Insert Multiple Documents".
"name": "John Doe",
"age": 35,
"position": "Project Manager",
"department": "IT",
"salary": 95000,
"joining_date": "2018-09-15"
},
"name": "Alice Brown",
"age": 30,
"position": "HR Manager",
"department": "Human Resources",
"salary": 70000,
"joining_date": "2020-02-20"
II. Reading (Retrieving) Documents
Finding All Employees
1. Click on the employees collection.
2. Click "Find" to display all documents.
Finding Employees in the IT Department
In the Filter box, enter:
{ "department": "IT" }
Finding Employees Earning More Than 80,000
In the Filter box, enter:
{ "salary": { "$gt": 80000 } }
Finding a Specific Employee
In the Filter box, enter:
{ "name": "John Doe" }
III Updating a Single Employee's Salary
1. Click "Update" → "Update One".
2. In Filter, enter:
{ "department": "IT" }
3.In Update, enter:
{ "$set": { "department": "Technology" } }
IV. Deleting Documents
Deleting a Single Employee
1. Click "Delete" → "Delete One".
2. In Filter, enter
{ "name": "Alice Brown" }
Deleting All Employees in the HR Department
1. Click "Delete" → "Delete Many".
2. In Filter, enter
{ "department": "Human Resources" }
Example 3:
/*Insert a Single Student Record:
"name": "Michael Johnson",
"age": 21,
"major": "Computer Science",
"gpa": 3.8,
"enrollment_year": 2022,
"courses": ["Data Structures", "Algorithms", "Database Systems"]
/*Insert Multiple Students
"name": "Sarah Parker",
"age": 22,
"major": "Biology",
"gpa": 3.6,
"enrollment_year": 2021,
"courses": ["Genetics", "Microbiology", "Biochemistry"]
},
"name": "David Lee",
"age": 23,
"major": "Mathematics",
"gpa": 3.9,
"enrollment_year": 2020,
"courses": ["Calculus", "Linear Algebra", "Probability"]
]
II. Read (Retrieve Student Data)
/*Find All Students
• Click on the students collection and click "Find".
/*Find Students in Computer Science
{ "major": "Computer Science" }
/*Find Students with GPA Greater Than 3.7
{ "gpa": { "$gt": 3.7 } }
/* Find a Specific Student
{ "name": "David Lee" }
Reference:
https://www.mongodb.com/docs/manual/tutorial/update-documents/