Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -MongoDB
- Database
- Collection - iWantToGoThere
- Objects
- Fields
- Values
- -MySQL
- Database
- Table
- Columns
- Rows
- //MySQL query
- INSERT INTO users (user_id, age, status)
- VALUES ('bcd001', 45, 'A')
- //mongoDB query
- db.users.insert({
- user_id: 'bcd001',
- age: 45,
- status: 'A'
- })
- //mySQL query
- SELECT * FROM users
- db.users.find()
- UPDATE users SET status = 'C'
- WHERE age > 25
- //mongoDB query
- db.users.update(
- { age: { $gt: 25 } },
- { $set: { status: 'C' } },
- { multi: true }
- )
- db.iWantToGoToThere.insert({"continent": "Africa", "country":"Morocco", "majorcities": ["Casablanca", "Fez", "Marrakech"]})
- db.iWantToGoToThere.find().pretty()
- // Find specific data by matching a field.
- db.iWantToGoToThere.find({"continent": "Africa"})
- db.iWantToGoToThere.find({"country": "Morocco"})
- 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'})
- // 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"]}})
- / 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";
- //multi:true updates all where country: morrocco
- 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()
- db.classroom.remove({name: [name of another neighbor]})
- db.classroom.update({}, {$set: {gavecandy:false}}, {multi:true})
- db.classroom.update({name:'Steve'}, {$set: {gavecandy:true}})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement