// 11/20/24 CSCI 4333.1 // [3] Show all student names. Return an array of student objects. db.student.find({}, { "fname": 1, "lname":1, "_id": 0 } ) // db.collection.find(query, projection, options) // query: {} // projection: { "fname": 1, "lname":1, "_id": 0 } ; 1: true; 0: false /* [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() 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"])) // [5] Show the names and credits (ach) of students majoring in 'CSCI' and having 40 or more credits. db.student.find( { "major": "CSCI", "ach" : {$gte: 40} }, { "fname": 1, "lname":1, "ach":1, "_id": 0 } ) db.student.find( { "major": "CSCI", "ach" : {$gte: 40} }, { "fname": 1, "lname":1, "major": 1, "ach":1, "_id": 0 } ) // query: { "major": "CSCI", "ach" : {$gte: 40} }; ach >= 40 // [6] Show the first name and last name of students with a first name starting with a L or B, case insensitive. db.student.find( { "fname": { $regex: /^[lb]/, $options: "i" } }, { "fname": 1, "lname":1, "_id": 0 } ) // query: { "fname": { $regex: /^[lb]/, $options: "i" } } // need to have the pattern/regular expression /^[lb]/ in "fname" // pattern: ^[lb]; ^: beginning of string location. [lb]: char class matches 1 char: l or b //[7] Show the names and credits (ach) of students majoring in 'CSCI' and having 40 or more credits. db.student.find( { "$and": [ { "major": "CSCI"}, { "ach": {"$gte": 40}} ] }, { "fname": 1, "lname":1, "ach":1, "_id": 0 } ) db.faculty.aggregate([ {"$group" : {_id:"$deptCode", "count":{$sum:1}}} ]) db.faculty.aggregate([ {"$group" : {_id:"deptCode", "count":{$sum:1}}} ]) db.faculty.aggregate( [ { $group: { "_id": "$deptCode", "count": {$sum:1}} }, { $project: { "deptCode": "$_id" , "num_faculty": "$count", "_id": 0}} ] ) db.student.aggregate([ {$lookup: { from: "enroll", let: {joinValue: '$stuId'}, pipeline: [ { $match: { $expr: { $and: [ { $eq: [ "$stuId", "$$joinValue" ] }, { $eq: [ "$classId", 10000 ] } ] } } } ], as: "enrollment" }}, { $match: {"enrollment": { $ne: [] }}}, { $project: { "fname": 1, "lname": 1, "_id": 0}} ]) // 11/18/24 CSCI 4333.1 db.test1.insertOne( { "StudentId" :1, "StudentName" : "Joseph Connor" } ) db.test1.find() db.test1.insertOne // remove tinker show dbs db.dropDatabase() show dbs use tinker // create index db.test1.createIndex( { "StudentId": 1 }, { unique: true } ) doc = { "StudentId" :1, "StudentName" : "Joseph Connor" } doc db.test1.insertOne(doc) db.test1.insertOne(doc) doc2 = { "StudentId" :2, "StudentName" : "Susan Connor" } db.test1.insertOne(doc2) db.test1.insertMany([ { "StudentId" :5, "GPA": 3.72 }, { "StudentId" :3, "GPA": 1.69, "x": "whatever will be, will be" }, { "BCAssetId": "78c22fc6-5dec-11ec-bf63-0242ac130002", "BCAssetType": "BCAssetTypeMetadata", "BCAssetName": "BCAssetTypeMetadata: MBSEModel", "ForBCAssetType": "MBSEModel", "Version": { "Version": "1.0", "Subversion": null, "StartTime": "2019-01-13T07:23:13+06:00" } } ]) db.test1.find() use toyu db.student.find() // Show all information of students majoring in 'CINF'. // db.collection.find(query: {"major": "CINF"}, projection: { "_id": 0 }, options) db.student.find({"major": "CINF"}, { "_id": 0 } ) db.student.find({"major": "CINF"}) db.student.find({"major": "CINF"}, { "_id": false } )