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.collection.find(query, projection, options db.student.find( {}, // query: compare to the WHERE clause in SQL { "_id": 0 } // projection; 0: false; compare to the SELECT clause in the SELECT statement. ) 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.student.find( {"majo": "CSCI", "minor":"CINF"}, // query { "_id": 0, "stuId": 1, "major": 1, "minor":1 } // projection; 0: false ) // Returns: A cursor to the documents that match the query criteria. When the find() method "returns documents," the method is actually returning a cursor to the documents. 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. // 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 } ) db.student.find( // return an empty cursor. { "major": "CSCI", "ach" : {gte: 40} }, { "fname": 1, "lname":1, "major": 1, "ach":1, "_id": 0 } ) // [6] Show the first name and last name of students with a first name starting with a L or B, case insensitive. // SELECT s.fname, s.lname FROM student AS s WHere s.fname REGEXP '^[lb]'; db.student.find( // $regex: need to match the regular expression. // regex is embedded by two /'s. // actual regex: ^[lb] // ^ matches the beginning position. // [lb]: character class: matches 1 char { "fname": { $regex: /^[lb]/, $options: "i" } }, { "fname": 1, "lname":1, "_id": 0 } ) // 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 } ) // Show the names and credits (ach) of students [1] majoring in 'CSCI' and having 40 or more credits, or [2] majoring in 'CINF' db.student.find( { "$or": [{ "$and": [ { "major": "CSCI"}, { "ach": {"$gte": 40}} ] }, { "major": "CINF"}]} , { "fname": 1, "lname":1, "major": 1, "ach":1, "_id": 0 } ) // SELECT DISTINCT s.fname, s.lname, s.major, s.ach FROM Student AS s WHERE (s.major = 'CSCI' AND s.ach >= 40) OR s.major = 'CINF'; db.faculty.find( { "deptCode": {"$in": ["CINF", "ITEC"] }}, { "faculty": {"concat": ["$fname", " ", "$lname"]}, "deptCode": 1, "rank": 1, "_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}} ])