DBS311 Adv_DB_Services_Week9_Lab8_MongoDB_Create_Delete
pdf
keyboard_arrow_up
School
Seneca College *
*We aren’t endorsed by this school
Course
311
Subject
Information Systems
Date
Jun 19, 2024
Type
Pages
8
Uploaded by PrivateEchidnaMaster2248
Lab 8 –
Week 9 file:///C/Users/jalpr/Downloads/DBS311%20%20%20Adv_DB_Services_Week9_Lab8_MongoDB_Create_Delete.html[2024-03-21 3:02:02 PM] Lab 8 –
Week 9 (MongoDB –
Create/Delete Database/Collection/Documents) Objective In this Lab, you learn to create and remove MongoDB Databases Collections Documents Getting Started Open your Windows command prompt and go the following directory where MongoDB is installed: C:\Program Files\MongoDB\Server\4.2\bin To run MongoDB, execute mongod mongod When MongoDB starts successfully, open another Windows command prompt and go the same bin directory: C:\Program Files\MongoDB\Server\4.2\bin and execute mongo mongo This opens the command line shell where you can work with MongoDB. Or you can create a batch file: Create a new text file named start_mongodb.bat using a text editor and save the following command in it: start call "C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" Start call "C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe" Every time you want to run MongoDB and open the client shell, execute this file.
Lab 8 –
Week 9 file:///C/Users/jalpr/Downloads/DBS311%20%20%20Adv_DB_Services_Week9_Lab8_MongoDB_Create_Delete.html[2024-03-21 3:02:02 PM] To test you solutions before you submit, test them individually and make sure they work correctly. You submit this file with answers (in the provided spaces). Name the file “L08_ID#_LASTNAME.pdf”.
Tasks 1. this question you create a new database named seneca and a collection student. We store student data in this collection. 2. use seneca This command makes “seneca”
your current database. However, the database will not be created until you insert a document into this database. db.collection_name.insertOne({}) Insert a new document into your collection student with the following data: first_name: Sarah last_name: Stone email: s_stone@email.com city: Toronto status: full-time gpa: 3.4 program: CPA 2. Write a command to check if the document has been created successfully. You use find() method to search and fetch documents. See the following example: student.find() { "_id" : 3, "name" : "Bao Ziglar", "scores" : [ { "score" : 71.64343899778332, "type" : "exam" }, { "score" : 24.80221293650313, "type" : "quiz" }, { "score" : 42.26147058804812, "type" : "homework" } ] } { "_id" : 6, "name" : "Jenette Flanders", "scores" : [ { "score" : 37.32285459166097, "type" : "exam" }, { "score" : 28.32634976913737, "type" : "quiz" }, { "score" : 81.57115318686338, "type" : "homework" } ] } { "_id" : 0, "name" : "aimee Zank", "scores" : [ { "score" : 1.463179736705023, "type" : "exam" }, { "score" : 11.78273309957772, "type" : "quiz" }, { "score" : 35.8740349954354, "type" : "homework" } ] } { "_id" : 8, "name" : "Daphne Zheng", "scores" : [ { "score" : 22.13583712862635, "type" : "exam" }, { "score" : 14.63969941335069, "type" : "quiz" }, { "score" : 75.94123677556644, "type" : "homework" } ] }
Lab 8 –
Week 9 file:///C/Users/jalpr/Downloads/DBS311%20%20%20Adv_DB_Services_Week9_Lab8_MongoDB_Create_Delete.html[2024-03-21 3:02:02 PM] { "_id" : 3, "name" : "Bao Ziglar", "scores" : [ { "score" : 71.64343899778332, "type" : "exam" }, { "score" : 24.80221293650313, "type" : "quiz" }, { "score" : 42.26147058804812, "type" : "homework" } ] } { "_id" : 6, "name" : "Jenette Flanders", "scores" : [ To see the result in JSON format, you can run the following statement: student.find().forEach(printjson)
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
Lab 8 –
Week 9 file:///C/Users/jalpr/Downloads/DBS311%20%20%20Adv_DB_Services_Week9_Lab8_MongoDB_Create_Delete.html[2024-03-21 3:02:02 PM] How many fields are in your document? 8
there any new field added to your document? yes
yes, what is the name of the field? id
3. Write a command to remove the document that you created in Question 1. (We have only one document at this time, but when removing documents make sure you will not remove some other documents if not needed. So, make sure your command will remove “Sarah Stone” from your collection. For now, we assume that we do not have duplicate names in our database.) Note: To avoid making mistakes, you can first write a find command with the proper criteria to see if the required document is fetched. Then, you can use the same criteria in your delete statement. (Write the statement to remove “Sarah Stone” from the database in the box below.)
db.student.deleteOne({ first_name:"Prince" })) What is the message as a result of your delete statement? Copy the message in the following box: { acknowledged: true, deletedCount: 1 } { "score" : 37.32285459166097, "type" : "exam" }, { "score" : 28.32634976913737, "type" : "quiz" }, { "score" : 81.57115318686338, "type" : "homework" } ] }
Lab 8 –
Week 9 file:/
O
//C
r
/
,
U
y
se
o
rs
u
/ja
c
lp
a
r/
n
Do
w
w
r
n
i
l
t
o
e
ad
t
s
h
/D
e
BS
d
3
b
11
o
%
r
20
d
%
b
2
.
0
g
%
e
2
t
0
N
A
a
dv
m
_D
e
B
(
_
)
S
s
er
t
v
a
ic
t
e
e
s
m
_W
e
e
n
ek
t
9
t
_
o
La
s
b8
e
_
e
M
t
o
h
ng
e
oD
n
B
a
_
m
Cr
e
eat
o
e_
f
D
y
e
o
le
u
te
r
.h
c
tm
u
l[
r
2
r
0
e
2
n
4-
t
03
d
-
a
21
ta
3:
b
0
a
2:
s
02
e
P
:
M] To see if the document is removed successfully, write a search statement to see if the document exists. (We look for one document not all). db.student.findOne() 4. want to add some students to our collection, but this time, we define the value for the _id field. (If the _id is not defined in your document, it will be added automatically.) _id: 1001 first_name: Sarah last_name: Stone email: s_stone@email.com city: Toronto status: full-time gpa: 3.4 program: CPA _id: 1002 first_name: Jack last_name: Adam email: j_adam@email.com city: North York status: part-time gpa: 3.6 program: CPA To add these students, we want to store these documents into a variable first. Define a variable named starray and add these two document to the variable. (You are storing more than one document so you need to define an array. Now, use the starray array to insert the documents to your collection student. Write your insert statement in the box be
l
o
_
w
:
({_id
.
:"1001", first_name:"Sarah", last_name:"Stone", email:"s_stone@email.com", city:"Toronto", status:"full-time", gpa:"3.4", program:"CPA"},{_id:"1002", first_name:"Jack", last_name:"Adam", email:"j_adam@email.com", city:"North York", Status:"part-time", gpa:"3.6", program:"CPA"}] What message is displayed after you execute the insert statement. Copy the message in the following box: [ { _id: '1001', first_name: 'Sarah', last_name: 'Stone', starray =
Lab 8 –
Week 9 file:/
O
//C
r
/
,
U
y
se
o
rs
u
/ja
c
lp
a
r/
n
Do
w
w
r
n
i
l
t
o
e
ad
t
s
h
/D
e
BS
d
3
b
11
o
%
r
20
d
%
b
2
.
0
g
%
e
2
t
0
N
A
a
dv
m
_D
e
B
(
_
)
S
s
er
t
v
a
ic
t
e
e
s
m
_W
e
e
n
ek
t
9
t
_
o
La
s
b8
e
_
e
M
t
o
h
ng
e
oD
n
B
a
_
m
Cr
e
eat
o
e_
f
D
y
e
o
le
u
te
r
.h
c
tm
u
l[
r
2
r
0
e
2
n
4-
t
03
d
-
a
21
ta
3:
b
0
a
2:
s
02
e
P
:
M] email: 's_stone@email.com', city: 'Toronto', status: 'full-time', gpa: '3.4', program: 'CPA' }, { _id: '1002', first_name: 'Jack', last_name: 'Adam', email: 'j_adam@email.com', city: 'North York', Status: 'part-time', gpa: '3.6', program: 'CPA' } ] Write a statement that shows all documents inserted in your collection student: db.student.find({})
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
Lab 8 –
Week 9 file:/
O
//C
r
/
,
U
y
se
o
rs
u
/ja
c
lp
a
r/
n
Do
w
w
r
n
i
l
t
o
e
ad
t
s
h
/D
e
BS
d
3
b
11
o
%
r
20
d
%
b
2
.
0
g
%
e
2
t
0
N
A
a
dv
m
_D
e
B
(
_
)
S
s
er
t
v
a
ic
t
e
e
s
m
_W
e
e
n
ek
t
9
t
_
o
La
s
b8
e
_
e
M
t
o
h
ng
e
oD
n
B
a
_
m
Cr
e
eat
o
e_
f
D
y
e
o
le
u
te
r
.h
c
tm
u
l[
r
2
r
0
e
2
n
4-
t
03
d
-
a
21
ta
3:
b
0
a
2:
s
02
e
P
:
M] 5. Write a statement to remove all documents in the student collection. db.seneca.deleteMany({}) 6. Write a statement to drop the database seneca. Use Seneca db.dropDatabase() Before dropping a database, make sure your current database is the one you want to delete. For this question, we want to delete (drop) the seneca database. You can write the use statement before removing the database to make sure your current database is seneca. use seneca
Lab 8 –
Week 9 file:///C/Users/jalpr/Downloads/DBS311%20%20%20Adv_DB_Services_Week9_Lab8_MongoDB_Create_Delete.html[2024-03-21 3:02:02 PM] getName() If your current database is not seneca, write the use statement to switch to seneca and then delete the database. What message is displayed after you execute the drop statement? Copy the message in the box below: { ok: 1, dropped: 'seneca' }