db.test1.insertOne( { "StudentId" :1, "StudentName" : "Joseph Connor" } ) db.test1.find() show dbs db.dropDatabase() show dbs // remove tinker use tinker db.test1.find() doc = { "StudentId" :1, "StudentName" : "Joseph Connor" } doc if (db.test1.find(doc).count() == 0) { db.test1.insertOne(doc) } db.test1.find() if (db.test1.find(doc).count() == 0) { db.test1.insertOne(doc) } db.test1.find() doc2 = { "StudentId" :1 } db.test1.find(doc2) db.test1.find() // remove tinker show dbs db.dropDatabase() show dbs // create index db.test1.createIndex( { "StudentId": 1 }, // 1st argument: fields in the index; 1: true { unique: true } ) // 2nd argument: property of the index doc = { "StudentId" :1, "StudentName" : "Joseph Connor" } doc db.test1.insertOne(doc) db.test1.insertOne(doc) db.test1.insertMany([ { "StudentId" :2, "GPA": 3.72 }, { "StudentId" :3, "GPA": 1.69 }, { "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() mongorestore --archive="toyu-db.gz" --gzip --nsFrom='toyu.*' --nsTo='toyu.*' db.student.find() db.student.find( {}, // query { "_id": 0 } // projection; 0: false ) db.student.find( {}, // query { "_id": 0, "studentId": 1, "major": 1 } // projection; 0: false ) db.student.find( {}, // query { "_id": 0, "stuId": 1, "major": 1 } // projection; 0: false ) db.student.find( {"major": "CSCI" }, // query { "_id": 0, "stuId": 1, "major": 1 } // projection; 0: false ) db.student.find( {"major": "CSCI", "minor":"CINF" }, // query { "_id": 0, "stuId": 1, "major": 1, "minor":1 } // projection; 0: false ) // db.collection.find(query, projection, options) // query: {} // projection: { "fname": 1, "lname":1, "_id": 0 } // Executing db.collection.find() in mongosh, returns a cursoe, automatically iterates the cursor to display up to the first 20 documents. Type it to continue iteration. 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"])) //Show the names and credits (ach) of students majoring in 'CSCI' and having 40 or more credits. // SQL .. WHERE major = 'CSCI' AND ach >= 40 db.student.find( { "major": "CSCI", "ach" : {$gte: 40} }, { "fname": 1, "lname":1, "major": 1, "ach":1, "_id": 0 } ) result = db.student.find( { "major": "CSCI", "ach" : {$gte: 40} },: [] { "fname": 1, "lname":1, "major": 1, "ach":1, "_id": 0 } ).toArray() // Show the first name and last name of students with a first name starting with a L or B, case insensitive. db.student.find( // a first name starting with a L or B, case insensitive. // fname needs to match a regular expression ($regex): // Regular expression is ^[lb] (regex is embedded by two /'s. // ^ match a position: beginning position // [lb]: a character class { "fname": { $regex: /^[lb]/, $options: "i" } }, { "fname": 1, "lname":1, "_id": 0 } ) use toyu // // [1] Show the stuId, classId and grade of all enrollment with a grade of "B+", "B", or "B-" in the following manner. // SQL: ... WHERE grade IN ("B+", "B", "B-") db["enroll"].find( { "grade": {"$in": ["B+", "B", "B-"] }}, { "stuId": 1, "classId": 1, "grade": 1, "_id": 0, } ) // [2] Show the name, rank, and department code of every CINF or ITEC faculty member in the following manner. // SQL: SELECT CONCAT(f.fname, ' ', s.lname) AS faculty, // f.deptCode, f.`rank` // FROM faculty AS f // WHERE f.deptCode IN ("CINF", "ITEC"); db.faculty.find( { "deptCode": {"$in": ["CINF", "ITEC"] }}, // query { "faculty": {"$concat": ["$fname", " ", "$lname"]}, "deptCode": 1, "rank": 1, "_id": 0, } // projection ) db.faculty.find() db.faculty.find( { "deptCode": {"$in": ["CINF", "ITEC"] }}, // query { "faculty": {"$concat": ["$fname", " ", "$lname"]}, "deptCode": 1, "rank": 1, "_id": 0, } // projection ) db.faculty.find( { "deptCode": {"$in": ["CINF", "ITEC"] }}, // query { "faculty": {"concat": ["$fname", " ", "$lname"]}, "deptCode": 1, "rank": 1, "_id": 0, } // projection ) // // [3] Show the classId and the number of all students enrolled in the class in the following manner. // db["enroll"].aggregate( [ { $group: { "_id": "$classId", "count": {$count: {}}} }, { $project: { "classId": "$_id" , "Number of students": "$count", "_id": 0 }} ] ) // // [4] Show the name, and deptCode of all faculty members who have the substring "an" in their first names or last names in JSON form in the following manner. // // query: { "$or": [ {"fname": { $regex: /an/, $options: "i" }}, // { "lname": { $regex: /an/, $options: "i" }}]}, result = db.faculty.find( { "$or": [ {"fname": { $regex: /an/, $options: "i" }}, { "lname": { $regex: /an/, $options: "i" }}]}, { "faculty": {"$concat": ["$fname", " ", "$lname"]}, "deptCode": 1, "_id": 0} ) result = db.faculty.find( { "$or": [ {"fname": { $regex: /an/, $options: "i" }}, { "lname": { $regex: /an/, $options: "i" }}]}, { "faculty": {"$concat": ["$fname", " ", "$lname"]}, "deptCode": 1, "_id": 0} ).toArray() db.faculty.find() 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.faculty.aggregate( [ { $group: { "_id": "$deptCode", "count": {$sum:1}} }, { $project: { "deptCode": "_id" , "num_faculty": "count", "_id": 0}} ] ) db["enroll"].aggregate( [ { $group: { "_id": "$classId", "count": {$count: {}}} }, { $project: { "classId": "$_id" , "Number of students": "$count", "_id": 0 }} ] )