DBS311_Lab09_MongoDB_Winter_2023
pdf
keyboard_arrow_up
School
Seneca College *
*We aren’t endorsed by this school
Course
DBS311
Subject
English
Date
Jan 9, 2024
Type
Pages
5
Uploaded by BrigadierWolverineMaster809
DBS311 – Advanced Data Services Winter 2023 1 | P a g e Lab 9 – Week 11 DBS311V1B.01983.2231
(MongoDB – UPDATE) Instructor Name & Email: Slavica O’Connor: Slavica.oconnor@senecacollege.ca
Due date is Saturday, April 8
th
, 11:59pm
Objective In this lab, students learn how to update documents in a MongoDB database. update():
This method updates one document by default. If you want to update all documents that match the criteria using this method, you need the option {multi:true}. update(<filter>,<update>,<option>) The filter
parameter specifies the criteria. For instance: {“_id”= 0} {} for updating all documents The update
parameter specifies the changes that will be applied to a document. updateOne():
This method updates only the first document that matches the criteria. updateOne(<filter>,<update>) updateMany():
This method updates all documents that match the criteria. updateMany(<filter>,<update>) Getting Started In this lab, you will use students.json dataset. Download students.json
and also insert_students.json
from Blackboard and store it in a folder named dataset. Open your Windows command prompt and go the following directory where MongoDB is installed:
DBS311 – Advanced Data Services Winter 2023 2 | P a g e Open your Windows command prompt and go the following directory where MongoDB is installed:
cd C:\Program Files\MongoDB\Server\6.0\
bin
To run MongoDB, execute mongod
mongod When MongoDB starts successfully, open another Windows command prompt and go the same bin
directory:
cd C:\mongosh\
mongosh-1.8.0-win32-x64\mongosh-1.8.0-win32-x64
\bin and execute mongosh
mongosh Or you execute a batch file to start up MongoDB. You will import products.json to the inventory
database. To import data, go to the MongoDB Compass. Use MongoDB Compass to: Create Database college
& collection students
Load students.json
file into collection students
Alternatively, you can open file insert_students.json and copy and paste content of it in your mongosh session. Either way you should get 27 documents in the students
collection.
show dbs You should see the database college
added to the list of your databases. To see the documents inside the database:
use college
db.students.countDocuments()
db.students.find().forEach(printjson) or
DBS311 – Advanced Data Services Winter 2023 3 | P a g e
db.students.find().pretty() If you need to drop students collection and re-load documents, use
db.students.drop() Submission You submit this file with answers (in the provided space). Name the file L09_ID#_LASTNAME.docx”. Tasks 1.
Write an update statement to add new fields program and term to all documents in the students
collection and set them to values "
CS
"
and 2. Verify your update. 2.
Write an update statement to modify the value of the program
field to “
Computer Science”
for all documents in the students
collection. Verify your update. 3.
Write an update statement to modify the value of the program
field/key to "
Math and Computer Science
"
for the student named Tambra Mercure
. Before executing an update statement or a delete statement, you can use the find()
method with the update or delete criteria, to see how many documents will be affected. Write the update statement in the box below. Verify your update.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
DBS311 – Advanced Data Services Winter 2023 4 | P a g e How many documents are there with the value Tambra Mercure
for the name
field? _______ How many documents were updated? ________ 4.
The students are about to write an assignment. Each student will be assigned to a team. Students with i) _id 1 to5 will be in team 1; ii) _id 6 to 10 will be in team 2; iii) _id 11 to 15 will be in team 3; iv) _id 16 to 20 will be in team 4, v) _id 21 to 25 will be in team 5. Write update statements to add field/key team
and set it to a value based on their _id. Verify your updates. How many update statements did you need to write? _______ How many documents each of the statements updated? ________ 5.
The students completed assignments. The professor marked assignments and gave scores to each team. i) team 1 received score 91.50, ii) team 2 received score 88.50, iii) team 3 received score 93.50, iv) team 4 received score 95.50, v) team 5 received score 100. Write update statements to add a new element to the array scores
to reflect a score for this assignment. The new element should have a similar format to the scores for exam, quiz, and homework. The value of the score will be the same for all students in the same team for the assignment. Verify your updates. Make sure that every student received mark for their assignments. How many update statements did you need to write? _______ How many documents each of the statements updated? ________ 6.
Run the following statement that will add an array for bonus quizzes to the student with _id 5 / name Wilburn Spiess
. db.students.update( { _id: 5 }, {"$set" : {"bonus_quizzes" : [ { "quiz": 1, "score" : 100 }, { "quiz": 2, "score" : 80 }, { "quiz": 3, "score" : 40 },
DBS311 – Advanced Data Services Winter 2023 5 | P a g e { "quiz": 4, "score" : 70 } ]}} ) Write a statement that will add the following five quizzes to the array bonus_quizzes
to the same student (i.e., Wilburn Spiess), sort all bonus quizzes and save only top 5 quizzes. Verify your statement. i)
"quiz": 5, "score" : 80 ii)
"quiz": 6, "score" : 30 iii)
"quiz": 7, "score" : 40 iv)
"quiz": 8, "score" : 99 v)
"quiz": 9, "score" : 85 How many update statements did you need to write? _______ How many documents each of the statements updated? ________