# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/Sangee-28/TaskMongoDB/blob/main/Task_Mongo_Db.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="RXba5v1a0aQ9"
# ! pip install dnspython
# ! pip install "pymongo[srv]"

# + id="4oWQQzVj2HbA"
#importing required modules

import pymongo
import re 
import pandas as pd
import json
from pymongo import MongoClient

#creating connection with MongoDB
client = pymongo.MongoClient("mongodb+srv://Sangeetha:SangiScooby28@<EMAIL>.<EMAIL>.<EMAIL>/myFirstDatabase?retryWrites=true&w=majority")


#  creating database 
db = client["TaskMongoDb"]

db = client.TaskMongoDb

records=db.students_data
   


students = []
for line in open('students.json', 'r'):
    students.append(json.loads(line))

#x= records.insert_many(students)
  




# + id="wWWj80ii_9aW"
#finding the name of the student who scored high marks in exam , quiz and Homework


#creating three different lists to store the marks of all students as per their category

exam=[]
quiz=[]
Homework=[]
for i in range(0,200):
  xx=students[i]['scores']
  exam.append(xx[0]['score'])
  quiz.append(xx[1]['score'])
  Homework.append(xx[2]['score'])


#finding max score for exam
maxscore_exam =max(exam)
maxscore_exam_index=exam.index(maxscore_exam)
#print(maxscore_exam)
#print(maxscore_exam_index)

#finding max score for quiz

maxscore_quiz =max(quiz)
maxscore_quiz_index=quiz.index(maxscore_quiz)
#print(maxscore_quiz)
#print(maxscore_quiz_index)

##finding max score for Homework
maxscore_Homework =max(Homework)
maxscore_Homework_index=Homework.index(maxscore_Homework)
#print(maxscore_Homework)
#print(maxscore_Homework_index)


#finding average:
avg=[]
for i in range(0,200):
  sum=exam[i]+quiz[i]+Homework[i]
  average=sum/3
  avg.append(average)
max_average =max(avg)
max_average_index=avg.index(max_average)
#print(max_average_index)

   

#find the name of the student:
id=[]
for i in range(0,200):
  id.append(students[i]['_id'])

Maximum_scorer =id[max_average_index]

#print("Maximum scorer in all three subjects :",Maximum_scorer)

#Querying in mongodb 

#print(students)

for doc1 in records.find({"_id":13}):
   print(doc1)






# + id="w7gV5yi-Hfpa"
# Find students who scored below average in the exam and pass mark is 40%?
sume=0
for i in exam:
  sume=sume+i

avge=sume/200


#print(avge)

id_s =[]
for i in range(0,200):
  if exam[i] < avge:
    id_s.append(i)




for i in id_s:
 for doc2 in records.find({"_id":i}):
   print(doc2)

  



# + id="QHxKq2hSj8hd" colab={"base_uri": "https://localhost:8080/"} outputId="1bb83695-0832-4cd1-d7c0-8000ec2b892c"
# Find the total and average of the exam, quiz and homework and store them in a separate collection

Total_average=[]



for i  in range(0, 200):
 sum=exam[i]+quiz[i]+Homework[i]
 average=sum/3
 d={'name':students[i]['name'],'total':sum, 'percentage': average}
 Total_average.append(d)

records=db.Total_average
records.insert_many(Total_average)


# + id="RawBDmDMhuOc" colab={"base_uri": "https://localhost:8080/"} outputId="ba9e119d-d696-4cd0-abdb-99aee9153da9"
#Creating a new collection to store the students who have failed in all the categories

Failed_in_all_categories=[]

for i  in range(0, 200):
  if (exam[i]<40) and (quiz[i]<40) and (Homework[i]<40):
    d1={'name':students[i]['name'] , 'id':students[i]['_id'], 'grade':'Fail in all categorie'}
    Failed_in_all_categories.append(d1)

print(Failed_in_all_categories)
    
records=db.failedinAllCategories
records.insert_many(Failed_in_all_categories)


# + id="ul_an4j_xSHS"
Pass_in_all_categories=[]

for i  in range(0, 200):
  if (exam[i]>=40) and (quiz[i]>=40) and (Homework[i]>=40):
    d1={'name':students[i]['name'] , 'id':students[i]['_id'], 'grade':'Pass in all categorie'}
    Pass_in_all_categories.append(d1)

print(Pass_in_all_categories)

records=db.PassinAllCategories
records.insert_many(Pass_in_all_categories)


StarcoderdataJupyterScriptsDedupFiltered