3/21/2017 - 12:53 AM

mongo queries

mongo queries

<!-- //Example w/ solution -->
use classroom
db.classroom.insert({name: 'Steve', row:3, os:'Mac', hobbies:['Coding', 'Reading', 'Running'] })

<!-- // B. Use find commands to get:
// 1. A list of everyone in your row. -->
// 2. An entry for a single person. -->

<!-- // 3. The entries for all the Mac users in your row.  -->
db.classroom.find({name:'Steve', row:3, os:'Mac'})
// Bonus:
// If you finish early, check out the MongoDB documentation 
//and figure out how to find users by an entry in an array. -->
db.classroom.find({"hobbies": {$in: ["hobby1"]}})


Make sure you are in your example db from 18.1.1
use lessondb

// Show how to update data 
// using db.[COLLECTION_NAME].update()

db.iWantToGoToThere.update({"country": "Morocco"}, {$set: {"continent":"Antartica"}})
// Note that the above will only update the first entry it matches.

// To update multiple entries, you need to add {multi:true}

    UPDATE iWantToGoToThere 
    SET continent = "Antartica"
    WHERE country = "Morocco";

db.iWantToGoToThere.update({"country": "Morocco"}, {$set: {"continent":"Antartica"}}, {multi:true})

// Ask the class what they think will happen when you run this command,
// even though a capital doesn't exist
db.iWantToGoToThere.update({"country": "Morocco"}, {$set: {"capital":"Rabat"}})
// answer: it will create the field

// And show the field can now be updated with the same command
db.iWantToGoToThere.update({"country": "Morocco"}, {$set: {"capital":"RABAT"}})

// Show how to push to an array with $push
db.iWantToGoToThere.update({"country": "Morocco"}, {$push: {"majorcities":"Agadir"}})

// Show how to delete an entry with db.[COLLECTION_NAME].remove()

// Show how to empty a collection with db.[COLLECTION_NAME].remove()

// Show how to drop a collection with db.[COLLECTION_NAME].drop()

// Show how to drop a database