Joeyrodrigues92
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. -->
db.classroom.find({row:3})
<!-- 
// 2. An entry for a single person. -->
db.classroom.find({name:'Steve'})

<!-- // 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
db
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()
db.iWantToGoToThere.remove({"country":"Morocco"})

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

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

// Show how to drop a database 
db.dropDatabase()