big data course homework 2
for each task write a single line in mongodb shell to get it done.
create a database named
refer to [[blk:mongodb/create database]]
HW.use HWcreate a collection named
students.db.createCollection('students')add atleast 10 documents to the
students collection. make sure the fields have sensible values. here is the structure of the documents you need to add:
- student id,
_id. - student name, including first name and last name.
- date of birth,
bdate. - list of courses the student studies
- course code,
ccode. - course name,
cname. -
grade.
- the field
phonesshould contain the phone numbers used to contact a student - the field
majorcontains the main field of study of the student. - the field
minorcontains the secondary field of study of the student.
db.students.insertMany((function(num) { mylist=[]; for (let i = 0; i < num; ++i) mylist.push({'_id': i, 'name': {'first': 'mahmood'+i, 'last': 'sheikh'}, 'bdate': new Date('2002-1-1'+i), 'courses': [{'ccode': '0', 'cname': 'algebra', 'grade': 10*i}], 'phones': ['052338801'+i, '052449912'+i], 'major': 'computer science', ...(i > 5) && {'minor': 'math'}}); return mylist; })(10));
write a command that shows all the students with all the data on them.
db.students.find().pretty()
for each student, show their name and the names of the courses they study, without id.
db.students.find({}, {'_id': 0, 'courses.ccode': 0, 'courses.grade': 0, 'bdate': 0, 'major': 0, 'minor': 0, 'phones': 0}).pretty()
show all students, for each student, show their id, a new field named
full name that contains both the first and the last name of the student. a new field year that contains the year the student was born. a new field age that shows the age of the student (use $$NOW,$subtract), the phone number field with a single number, and a new field named #courses whose value is the number of courses a student has.db.students.find({}, {'full name': {$concat: ['$name.first', ' ', '$name.last']}, 'year': {$dateToString: {'date': '$bdate', 'format': '%Y'}}, 'age': {$toInt: {$divide: [{$subtract: ['$$NOW', '$bdate']}, 1000*60*60*24*365]}}, '#courses': {$size: '$courses'}, 'phones': {$slice: 1}}).pretty()
show all students, for each student show their id, the number of courses they're in and the number of phones they own. in addition, show their major and minor field of studies joined by a forward slash, in case they dont have a secondary field of study show the main field without a slash.
db.students.find({}, {'full name': {$concat: ['$name.first', ' ', '$name.last']}, '#courses': {$size: '$courses'}, '#phones': {$size: '$phones'}, 'field of study': {$cond: [{$eq: ["$minor", undefined]}, "$major", {$concat: ["$major", "/", "$minor"]}]}}).pretty()
show the details of the students with a specific id of your choice.
db.students.find({'_id': 1}).pretty()
show the details of all the students except one with a specific id of your choice.
db.students.find({'_id': {$ne: 3}}).pretty()
show the details of 3 students with specific ids of your choice.
db.students.find({'_id': {$nin: [1, 2, 3]}}).pretty()
show the details of the students whose date of birth is within the span of two dates of your choice.
db.students.find({bdate: {$gte: new Date("2000-01-01"), $lte: new Date("2005-01-01")}});
show all the students that are in a specific course of your choice.
db.students.find({"courses.cname": "algebra"});
show all the students that are in a specific course of your choice whose grades are above 80, or some other number of your choice.
db.students.find({"courses.cname": "algebra", "courses.grade": {$gt: 70}});
show all the students with only one phone number.
db.students.find({"phones": {"$size": 2}});
show the student with a specific phone number of your choice.
db.students.find({"phones": "0524499128"});
show the number of students that are in a collection by storing the results in a
cursor variable with usage of count.var myCursor = db.students.find({}); myCursor.count()