Sunteți pe pagina 1din 9

Team Members

Kanagajothi V 16bcs090

Sharmila Gomathi M 16bcs102


Priyadharshini A

Pavithra M

Ramya Sri

Students Mark Analysis DB


ABC Engineering College decided to automate the process of student mark
analysis in all departments. A student can be associated with only one department. A
Course can be associated with more than one department and it can be associated with
any semester. Marks secured by each student in each subject is documented
separately. Result has to be published for each student separately.

So development team planned to maintain a database for Student Mark Analysis


Database Management System something like below: (partial schema. Not a fixed
Stschema)

1. Students(sid, sname, dept_id, semester, address, phno)


2. Departments(dept_name, dept_id, building, block, no_of_semesters)
3. MarksSecured(course_id, stud_id, marks)
4. Courses(course_id, course_name,dept_id, semester, credits)
5. FinalResult(stud_id, no_of_arrears, course_id_Of_Arrears, cgpa)

Questions:

For the above scenario, Write a query to

1. Testing team decides to add 6 documents in all collections that satisfies all
needed integrity constraints
Db.Students.insert({sid:”102”,sname:”Kanaga”,dept_id:”112”,semester:6,address:
”Madurai”,phno:7339531327});
2. Display the contents in all the collections
Db.Students.find().pretty();
Db.Departments.find().pretty();
3. One of the student in the college with his ID as 1012 decided to change his name
from ‘Rama’ to ‘Ram’ and semester to 4. Do case insensitive search
Ans: db.Student.update({_sid:"s1012"},{$set:{sname:"ram",semester:4}});

4. Remove all the details added in ‘FinalResult’ relation


Ans:db.FinalResult.remove();
5. Retrieve marks secured information for the students with student id 1013, 1014,
1015 for the courses with course id 101,102,103.
Ans:db.MarksSecured.find({stud_id:[‘1013’,’1014’,’1015’]},{marks:1});
Ans:db.MarksSecured.find.({course_id:[‘101’,’102,’103’]},{marks:1});

6. College decided to change the course name of course with courseid 405 as
‘Advanced DBMS’

Ans:
db.Courses.update({course_id:"15cs301"},{$set:{course_name:"Advanced
DBMS"}});

7. Retrieve student id, student name, department name and cgpa for that student
Ans:db.Student.find({sid:1,sname:1,dept_name:1,cgpa:1});
8. College decided to change the credits to 4 for the following courses: 101, 504,
307, 222, 608

Ans:
db.Courses.update({course_id:['15cs301','15ci301','15bt301']},{$set:{credits:4}}
);
9. Retrieve all student information for whom the result has yet to be published
Ans:db.FinalResult.find({course_id_of_Arrears:”nil”});
10. Remove all the information from FinalResult relation, for all students who ids
starts with either 1 or 2.

Ans: db.FinalResult.remove({},{stud_id:{$in:[1,2]}});

11. Remove all course related information for the department with department id 3
and semester 8
Ans: db.Department.remove({dept_id:3,sem:8});

12. Retrieve all student details whose phone number contains 2 as 2 nd digit and 4 as
4th digit

Ans: db.Students.find({$and:[{phno.2:’2’},{phno.4:’4’}]});

13. Retrieve course id, course name and credits for all courses, whose course name
starts with ‘M’. Do case insensitive search
Ans: db.Courses.find({$or:[{"course_name":/^M/},{"course_name":/^m/}]});
14. College decided to invite all parents for parents-teachers meeting. For which,
unique student details needs to be collected based on the place they are residing
Ans:db.Students.distinct(“address”);
15. Find the total number of deparments available in the college
Ans:db.Departments.count();
16. Retrieve all student information for whom the result is already published
Ans:db.FinalResult.find({course_id_of_Arrears:1});
17. College decided to view the hiked cgpa value of 2% for each student along with
student id, name, CGPA before hike and CGPA after hike as field name for the
resultant table
Ans:db.FinalResult
18. Find the total number of unique documents in the students relation based on
student name
Ans:db.Student.distinct(“sname”);
19. Retrieve all student details like id, name and CGPA for those whose are having
arrears in more than 2 papers and less than 5 papers
Ans:db.FinalResult.find({no_of_arrears:{$gt:2 , $lt:5},{stud_id:1,cgpa:1});
20. Generate (Retrieve) ranklist based on CGPA from FinalResult relation

Ans: db.Final_result.find().sort({cgpa:-1}).pretty();

21. Retrieve maximum CGPA from result

Ans:
db.Final_result.aggregate([{$group:{_id:"$cid",maxiCgpa:{$max:"$cgpa"}}}]);

22. Retrieve minimum CGPA form result

Ans: >
db.Final_result.aggregate([{$group:{_id:"$cid",miniCgpa:{$min:"$cgpa"}}}]);

23. Retrieve all student information, if the result is not published so far, then display
number of arrears and CGPA with value as -1.
Ans:db.Final_result.find({},{_id:0,sid:0,cgpa:1,no_of_arr:1}).sort({cgpa:1}).p
retty();
24. Retreive department information for those department whose building has more
than 3 blocks
Ans: db.Department.find({block:{$gt:3}}).pretty();
25. College decided to view all student information in the alphabetical order based on
their name
Ans: db.Student.find().sort({sname:-1}).pretty();
26. College decides to classify the students into three category as Good, Average
and Poor based on their performance in examination through CGPA. If the CGPA
is greater than or equal to 8 then Good. If CGPA is between 6.5 and 8 then
average, otherwise Poor. Write a query to do the same
Ans:
db.Final_result.aggregate([{$project:{_id:"$sid",Good:{$gte:["$cgpa",8]},Av
erage:{$gte:["$cgpa",6]},Poor:{$lt:["$cgpa",6]}}}]);
27. Using course_id_Of_Arrears attribute in the FinalResult collection maintain the
list of course ids failed by each student.
Ans: db.Final_result.find({},{sid:1,_id:0,cid_of_arr:1});
28. Insert a new array attribute named “course_name_Of_Arrears” attribute in the
FinalResult collection maintain the list of course names failed by each student.
Ans:
db.Final_result.update({"sid":"S103"},{$push:{cid_name:{$each:["Data
Structures","Big Data","PDS"]}}});
29. Retrieve only the department name and building for all departments which have
exactly 8 semesters.
Ans: db.Department.find({"No_Sem":8},{dep_Name:1,building:1,_id:0});
30. Retrieve only the student name, phone number and address for all students who
are having phone number.
db.Student.aggregate([{$project:{Sname:1,_id:0,addr:1,phoneno:{$ifNull:["
$phoneno" ,0]}}}]);
31. Find out the total number of credits for deptid 101.
Ans:
db.courses.aggregate([{$group:{_id:"$cid",sumOfCredits:{$sum:{"$credit"}
},count:{$sum:1}}}]);
32. Find out the total number of credits for deptid 101 and 122.
33. Display the department name in position five and six.
db.Department.find().sort({dept_id:1}).limit(2).skip(4);
34. Arrange the FinalResult collection in decreasing order based on CGPA.
Ans: db.Final_result.find().sort({Sid:-1})
35. Arrange the FinalResult collection in decreasing order based on CGPA and then
in increasing order based on no_of_Arrears.
Ans: db.Final_result.find().sort({cgpa:-1,no_of_arr:1})
36. Retrieve all student names that start with “Vi”.
Ans : db.Student.find({"Sname":/^vi/});
37. Retrieve all student names that end with “ai”
Ans: db.Student.find({"Sname":/ai$/});.
38. Retrieve all student names that start with “C”.
Ans: db.Student.find({"Sname":/^C/});
39. Retrieve all department names that contains “S” at any position.
Ans: db.Student.find({"Sname":/S/});
40. Modify the department name from “Phy” to “Physics”
.Ans:
db.Department.update({"dep_Name":"Phy"},{$set:{dep_Name:"Physics"}});
41. Update the marks by adding 10 to it for course ids 101, 102, 103, 108, 109.
Ans:
db.Marks_secured.update({"Cid":{$in:["C167","C193","C190","C198","C178
"]}},{$set:{"Marks":"Marks"+10}},{multi:true});
42. Display all student details who is in 4th, 5th or 6th semester and who is residing in
Bangalore.
Ans:
db.Student.find({"sem":{$in:["VI","V","IV"]},"addr.place":"Bangalore"});
43. Use upsert property properly with true and false values respectively and show
the changes made in student collection.
Ans:
db.Student.update({"_id":3,"Sname":"Jai","sem":"VI"},{$set:{"dept_id":"de
p102"}},{upsert:false});
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
db.Student.update({"_id":3,"Sname":"Jai","sem":"VI"},{$set:{"dept_id":"de
p102"}},{upsert:true});
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0,
"_id" : 3 })
44. Use save method in students collection and show the difference clearly.
Ans:
db.Student.save({"_id":4,"Sname":"meenu","sem":"VI","dept_id":"dep102"
});
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" :
4 })
db.Student.save({"_id":3,"Sname":"sabiya","sem":"VI","dept_id":"dep103"}
);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
45. Retrieve last 7 documents from the MarksSecured collection.
Ans: db.Marks_secured.find({}).sort({$natural:-1}).limit(7);
46. Retrieve only the student id and marks for last 5 documents.
Ans:
db.Marks_secured.find({},{"sid":1,"Marks":1,"_id":0}).sort({$natural:-
1}).limit(5);
47. Skip the first three department details and display the remaining.
Ans: db.Department.find().skip(3);
48. Find those documents from the FinalResult collection having arrears in course id
101, 102 in the course_id_Of_Arrears array.
Ans: db.Final_result.find({"cid_of_arr":{$in:["cid1","cid2"]}});
49. Display first two course_name_Of_Arrears starting from 1st index.
Ans: db.Final_result.remove({db.Final_result.find().sort({$natural:-
1}).limit(1)});

50. Remove the last course_name_Of_Arrears details from the array.

Ans: db.Final_result.remove({db.Final_result.find().sort({$natural:-
1}).limit(1)});
51. Remove all course_name_Of_Arrears details from the array for student id 106
.ans: db.Final_result.remove({sid:"S102"});
52. Display the total number of documents in the collection FinalResult.
Ans: db.Final_result.count();
53. Display the number of elements in the array course_name_Of_Arrears for the
candidate 105.
Ans: db.Final_result.find({sid:"S103"},{_id:0,cid_name:1});
54. Find all student id of students who has secured marks >60 and <80 and display
only the sid’s starting with 5 using aggregans:ate functions
Ans: db.Marks_secured.find({Marks:{$gt:60,$lt:100},sid:[/s1/]});.

55. Find all student id of students who has secured marks >60 and <80 and display
only the sid’s starting with 5 using mapreduce functions.
Var map = function(){emit(this.sid,this.marks);}
Var reduce = function(key,value){return Array.sum(values);}
db.Marks_secured.mapReduce(map,reduce,{out:”Marks”,query:[sid:/^5]});

56. Write a javascript programming to find whether the given number is palindrome
or not.
Ans: > var ispalindrome=function palindrome(str) { var len = str.length;
var mid = Math.floor(len/2); for ( var i = 0; i < mid; i++ ) { if (str[i] !==
str[len - 1 - i]) { return false; } } return true; }
> ispalindrome(1001);
true
57. Create a user-defined cursor to iterate over the Courses collection whose credit
is 2 or 4.
Ans: var myCursor=db.Courses.find({$or:{credit:2},{credit:4}});
While(myCursor.hasNext()){
Var myDoc=myCursor.next();
print(
58. Export the MongoDB result to a CSV file.
Ans: mongoexport –db studentMark –collection result –csv –fieldFile
d:/field.txt –out d:/output.txt
59. import a CSV file contains name, age, petname details as a collection in
MongoDB
Ans: mongoimport –db student –collection person –typeCSV –name –File
D:/sample.txt
60. Automatically generate id for a new collection of your own.
Ans:db.usercounters.insert(
{
_id:”empid”,seq:0});
function getnextseq(name){ var ret=db.usercounters.findAndModify(
{ query:{_id:name},update:{$inc:{seq:1}},new:true}); return ret.seq;
})
db.users.insert({ id:getnextseq(“empid”),”Name”:”sarah jane”})

S-ar putea să vă placă și