-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_student.py
More file actions
47 lines (41 loc) · 984 Bytes
/
insert_student.py
File metadata and controls
47 lines (41 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
import json
import pymongo
import sys
# connect to mongo
connection = pymongo.MongoClient("mongodb://localhost")
# get a handle to the reddit database
db = connection.student
grades = db.grades
# Read student id
s_name = input('student name:')
s_id = int(input('student id: '))
s_exam = float(input('student exam score:'))
s_homework = float(input('student homework score:'))
s_quiz = float(input('student quiz score:'))
# Define student
new_student = {
"_id": s_id,
"name": s_name,
"scores": [
{
"type": "exam",
"score": s_exam
},
{
"type": "homework",
"score": s_homework
},
{
"type": "quiz",
"score": s_quiz
}
]
}
# Retrieve student from mongodb
try:
student = grades.insert_one(new_student)
except Exception as e:
print("Unexpected error:", type(e), e)
sys.exit(0)
print("Student has been inserted!")