// 2024_4_17.js // Mongosh mongosh.exe // Set up toyu DB in Mongo. mongorestore --archive="toyu-db.gz" --gzip --nsFrom='toyu.*' --nsTo='toyu.*' // [1] Show all students. use toyu db.student.find() // db.collection.find(query, projection, options) // Getting rid of _id: // query: {} // projection: { "_id": 0 } db.student.find({}, { "_id": 0 } ) db.student.find({}, { "_id": false } ) students = db.student.find({}, { "_id": false } ) // Show all information of students majoring in 'CINF'. // query: {"major": "CINF"} db.student.find({"major": "CINF"}, { "_id": 0 } ) // Show all student names. Return an array of student objects. db.student.find({}, { "fname": 1, "lname":1, "_id": 0 } ) db.student.find({"major": "CINF"}, { "fname": 1, "lname":1, "major": 1, "minor": 1, "_id": 0 } ) [4] Show all student names in this format: student #0: Tony Hawk student #1: Mary Hawk student #2: David Hawk student #3: Catherine Lim student #4: Larry Johnson student #5: Linda Johnson student #6: Lillian Johnson student #7: Ben Zico student #8: Bill Ching student #9: Linda King Solution: result = db.student.find({}, { "fname": 1, "lname":1, "_id": 0 } ).toArray() // May not always work as toArray() returns a promise, // which may not be ready for use. result.forEach((x,i) => console.log('student #' + String(i) + ': ' + x["fname"] + ' ' + x["lname"])) result.forEach((x,i) => console.log('student #' + String(i+1) + ': ' + x["fname"] + ' ' + x["lname"])) result = db.student.find({"major": "CINF"}, { "fname": 1, "lname":1, "major": 1, "minor": 1, "_id": 0 } ).toArray() result.forEach((x,i) => console.log('student #' + String(i+1) + ': ' + x["fname"] + ' ' + x["lname"] + ' major in ' + x["major"]))