0% found this document useful (0 votes)
132 views6 pages

FIT3176 W4 Lab 03 Activity Sheet 1 MongoDB CRUD

1. The document provides instructions on performing basic CRUD operations using MongoDB and the Mongo Shell. It includes steps to connect to Mongo Shell, create a database and collection, and insert sample student documents. 2. The document then lists various read operations to query the student collection including finding students by course, campus status, location, and sorting results. 3. Update operations are outlined such as modifying student documents by changing courses, adding/removing fields, and pushing new array elements. 4. Delete operations are also provided including removing documents by campus status and enrollment year.

Uploaded by

eddweeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views6 pages

FIT3176 W4 Lab 03 Activity Sheet 1 MongoDB CRUD

1. The document provides instructions on performing basic CRUD operations using MongoDB and the Mongo Shell. It includes steps to connect to Mongo Shell, create a database and collection, and insert sample student documents. 2. The document then lists various read operations to query the student collection including finding students by course, campus status, location, and sorting results. 3. Update operations are outlined such as modifying student documents by changing courses, adding/removing fields, and pushing new array elements. 4. Delete operations are also provided including removing documents by campus status and enrollment year.

Uploaded by

eddweeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1

Lab 03 Activity Sheet 1


Getting Started with Mongo Shell
The objectives of this lab are to: 
1. To become familiar with the MongoDB environment, using Mongo Shell
2. To be able to do basic MongoDB CRUD operations

File Template for Answer

A file template (Week4_Activity_Code.js) is provided for you on Moodle (under the


Week 4 resources) to answer this tutorial exercise. You can use any code editor application
(e.g. Visual Studio Code, Pycharm etc.) to edit your answer in the provided file template.

Connecting to Mongo Shell

You can connect to Mongo Shell using either

MongoDB Compass’s MONGOSH


2

Using the Terminal (MacOS) / Command Prompt (Windows)

With either the mongo or mongosh commands

Using a VDI/Code Editor e.g. Pycharm

More details can be found on Moodle > Unit Information > Software Installation Guides
3

CRUD Operations using Mongo Shell

CREATE

1. Create a database called studentEnrolmentDB.


2. Create a collection called students.

3. Insert the following data into students collection:

ID First Last Address Gender Course Year Off- Email Add


name name Street Suburb State Postcode Campus
Address
1001 John Smith 123 Clayton VIC 3168 Male BITS 2019 false jsmith@gm
Monash jsmith@ya
Drive
1002 Mary Citizen 900 Caulfield VIC 3145 Female BSE 2018 true mcitizen@
Dandenong East
Road
1003 Fred Bloggs 90 Clayton VIC 3168 Male BITS, 2017 false fredb@gm
Wellington BBIS
Road
1004 Nick Nice 3 Robinson Kew VIC 3080 Male BCS 2018 false nicknice@g
Avenue
1005 Wendy Wheat 6 Algorithm Malvern VIC 3144 Female GDS 2019 true wwheat@y
Street

db.students.insertOne({
"_id": 1001,
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "123 Monash Drive",
"suburb": "Clayton",
"state": "VIC",
"postcode": 3168
},
"gender": "male",
"course": "BITS",
"year": 2019,
"offCampus": false,
"emailAddress": [
"[email protected]",
"[email protected]"
]
})

db.students.insertOne({
"_id": 1002,
"firstName": "Mary",
"lastName": "Citizen",
"address": {
"streetAddress": "900 Dandenong Road",
"suburb": "Caulfield East",
"state": "VIC",
"postcode": 3145
},
"gender": "female",
"course": "BSE",
"year": 2018,
4

"offCampus": true,
"emailAddress": "[email protected]"
})

db.students.insertOne({
"_id": 1003,
"firstName": "Fred",
"lastName": "Bloggs",
"address": {
"streetAddress": "90 Wellington Road",
"suburb": "Clayton",
"state": "VIC",
"postcode": 3168
},
"gender": "male",
"course": [
"BITS",
"BBIS"
],
"year": 2017,
"offCampus": false,
"emailAddress": "[email protected]"
})

db.students.insertOne({
"_id": 1004,
"firstName": "Nick",
"lastName": "Nice",
"address": {
"streetAddress": "3 Robinson Avenue",
"suburb": "Kew",
"state": "VIC",
"postcode": 3080
},
"gender": "male",
"course": "BCS",
"year": 2018,
"offCampus": false,
"emailAddress": "[email protected]"
})

db.students.insertOne({
"_id": 1005,
"firstName": "Wendy",
"lastName": "Wheat",
"address": {
"streetAddress": "6 Algorithm Street",
"suburb": "Malvern",
"state": "VIC",
"postcode": 3144
},
"gender": "female",
"course": "GDS",
"year": 2019,
"offCampus": true,
"emailAddress": "[email protected]"
})
5

READ
1. List all students in the students collection.

2. Find the students who are doing the BITS course.

3. Show all on-campus students.

4. List all students who live in Clayton.

5. List all male students who are enrolled after 2017.

6. Show all students who live in Malvern or Caulfield.

7. Display the name of all students and their course.

8. Show the name, course, and year enrolled for 2 students.

9. Display all students and sort them by their enrolment year.

10. Count the number of students who are doing BBIS. (Hint: count())

11. Count the number of students who are enrolled in between 2018 to 2019.

12. Find 3 students who are enrolled before 2019 and are doing either BITS or BSE.

13. Sort the student list based on their year enrolled in descending order, and display the name
of the top 3 students.

14. Show the students who are not doing BITS. Display only the names of those students and
sort their names in ascending order.

15. Find the students who are doing a double degree in BITS and BBIS.

UPDATE

1. Assuming Mary Citizen changes her course from BSE to BCS, update the collection to
reflect this change.

2. Add Wendy Wheat’s birthday, which is on 30th October 1990.

3. If you stored the email address for Nick Nice in a string, change it to an array and add a new
email address: [email protected].

4. Add another email address for Nick Nice: [email protected]. (Hint: $push)

5. Find students who are doing the BITS course and add these units:
● Unit Code: FIT3176, Unit Name: Advanced Database Design
● Unit Code: FIT2094, Unit Name: Databases
6

You have to ensure that each unit is stored as an object.

6. Remove the birthday field that we entered for Wendy Wheat. (Hint: $unset)

7. Replace the details of Fred Bloggs to the following:


Student ID: 1003
First name: Fred
Last name: Bloggs
Gender: Male
Course:
1. Course name: BITS, Year: 2017
2. Course name: BBIS, Year: 2018
Off-campus: false
Email: [email protected]

DELETE

1. Delete one student who is enrolled as an on-campus student.


2. Delete the students who are enrolled before 2019 and are off-campus students.

The End

You might also like