//Slide 17 – first connect to server and select a database to use //get connection by using string from Atlas mongo "mongodb+srv://cluster0-rq8e6.mongodb.net/cs61” --username cs61 show dbs db //test //switch to cs61 database use cs61 //Slide 18 – dropDatabase deletes a database //drop cs61 db.dropDatabase() //show db is still cs61 (even though dropped) db //returns cs61 //Slide 19 – createCollection makes a collection, like CREATE TABLE //create a new collection called Restaurants db.createCollection("Restaurants") //show all collections in database show collections //Slide 20 – insert documents into a collection with insert, list with find //insert restaurant into Restaurants collection db.Restaurants.insert({name:"Tim's Tasty Treats",boro:"Manhattan"}) //find restaurants – like SELECT db.Restaurants.find() db.Restaurants.find().pretty() //note _id not inserted, so Mongo created one, if had provided _id field, Mongo would use that instead of creating one //Slide 21- insert several documents at the same time by using a JSON array //insert several restaurants into Restaurants collection db.Restaurants.insert([{name:"Tim's Untasty Treats",boro:"Queens"},{name: "Brooklyn's Best Restaurant",boro:"Brooklyn"},{name:"Bronx diner",boro:"Bronx"}]) //show all restaurants db.Restaurants.find().pretty() //can also use insertOne and insertMany //Slide 22 – find returns documents matching the provided criteria //find restaurants in Manhattan db.Restaurants.find({boro:"Manhattan"}).pretty() //only Tim’s Tasty Treats //can also use $or (must put conditions in array) db.Restaurants.find({$or: [{boro:"Manhattan"},{boro:"Queens"}]}).pretty() Slide 23 – find can also project fields and limit the number of documents returned //return only the name of restaurants db.Restaurants.find({},{"name":1}).pretty() //get _id by default also, turn it off db.Restaurants.find({},{"name":1,_id:0}).pretty() //no _id field //add limit db.Restaurants.find({}).limit(2) //only two returned //Slide 24 – add sort to sort the results //use 1 to sort ascending, -1 to sort descending db.Restaurants.find({}).sort({boro:1}) //ascending by boro db.Restaurants.find({}).sort({boro:-1}) //descending by boro db.Restaurants.find({}).sort(name:1}.limit(2) //only first two returned //Slide 25 – update can add new fields or update existing fields //find restaurants db.Restaurants.find({}) //get _id for Tim’s Tasty Treats //add score field to Tim’s Tasty Treats – get _id from find! db.Restaurants.update({_id:ObjectId("5ebfe42a6ead6c5a3b84c554")},{$set:{score:5}}) //score field created on Tim’s //update score field to Tim’s Tasty Treats to have score of 6 db.Restaurants.update({_id:ObjectId("5ebfe42a6ead6c5a3b84c554")},{$set:{score:6}}) //Slide 26 – use remove to delete documents //add score to Bronx Diner – get _id from find! db.Restaurants.update({_id:ObjectId("5ebfe8c46ead6c5a3b84c558")},{$set:{score:12}}) //remove all restaurants with score > 6 db.Restaurants.remove({score: {$gt: 6}}) //Bronx Diner removed //use db.Restaurants.remove({}) to TRUNCATE //Slide 27 – Use createIndex to add an index on a field for faster lookup like a RDBMS //use getIndexes to see existing indexes db.Restaurants.getIndexes() //add index on boro db.Restaurants.createIndex({boro:1}) // 1 means ascending //drop indexes db.Restaurants.dropIndex({boro:1}) /* Slide 28 - Practice 1. Create a collection to hold data about Students where each Student has: • Name • Year */ db.Students.createCollection(“Students”) /* 2. Insert three students • Alice 20 • Bob 21 • Charlie 22 */ db.Students.insert([{name:"Alice",year:20},{name:"Bob",year:21},{name:"Charlie",year:22}]) //3. Add GPA attribute to Alice, give her a 3.5 db.Students.update({_id:ObjectId("5ec5146400766dcb02610ddb")},{$set:{GPA:3.4}}) //4. Find Students where year > 20 db.Students.find({year: {$gt:20}})