SHARE
TWEET

Untitled

a guest Mar 20th, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -MongoDB
  2. Database
  3. Collection - iWantToGoThere
  4. Objects
  5. Fields
  6. Values
  7.  
  8. -MySQL
  9. Database
  10. Table
  11. Columns
  12. Rows
  13.  
  14. //MySQL query
  15. INSERT INTO users (user_id, age, status)
  16. VALUES ('bcd001', 45, 'A')
  17.  
  18. //mongoDB query
  19. db.users.insert({
  20.   user_id: 'bcd001',
  21.   age: 45,
  22.   status: 'A'
  23. })
  24.  
  25. //mySQL query
  26. SELECT * FROM users
  27. db.users.find()
  28. UPDATE users SET status = 'C'
  29. WHERE age > 25
  30.  
  31. //mongoDB query
  32. db.users.update(
  33.   { age: { $gt: 25 } },
  34.   { $set: { status: 'C' } },
  35.   { multi: true }
  36. )
  37.  
  38. db.iWantToGoToThere.insert({"continent": "Africa", "country":"Morocco", "majorcities": ["Casablanca", "Fez", "Marrakech"]})
  39.  
  40. db.iWantToGoToThere.find().pretty()
  41.  
  42. // Find specific data by matching a field.
  43. db.iWantToGoToThere.find({"continent": "Africa"})
  44. db.iWantToGoToThere.find({"country": "Morocco"})
  45.  
  46. db.classroom.insert({name: 'Steve', row:3, os:'Mac', hobbies:['Coding', 'Reading', 'Running'] })
  47.  
  48. // B. Use find commands to get:
  49. // 1. A list of everyone in your row.
  50. db.classroom.find({row:3})
  51.  
  52. // 2. An entry for a single person.
  53. db.classroom.find({name:'Steve'})
  54.  
  55. // 3. The entries for all the Mac users in your row.
  56. db.classroom.find({name:'Steve', row:3, os:'Mac'})
  57.  
  58. // If you finish early, check out the MongoDB documentation
  59. // and figure out how to find users by an entry in an array.
  60. db.classroom.find({"hobbies": {$in: ["hobby1"]}})
  61.  
  62. / Show how to update data
  63. // using db.[COLLECTION_NAME].update()
  64.  
  65.  
  66. db.iWantToGoToThere.update({"country": "Morocco"}, {$set: {"continent":"Antartica"}})
  67. // Note that the above will only update the first entry it matches.
  68.  
  69. // To update multiple entries, you need to add {multi:true}
  70.  
  71.     UPDATE iWantToGoToThere
  72.     SET continent = "Antartica"
  73.     WHERE country = "Morocco";
  74.  
  75. //multi:true updates all where country: morrocco
  76. db.iWantToGoToThere.update({"country": "Morocco"}, {$set: {"continent":"Antartica"}}, {multi:true})
  77.  
  78. // Ask the class what they think will happen when you run this command,
  79. // even though a capital doesn't exist
  80. db.iWantToGoToThere.update({"country": "Morocco"}, {$set: {"capital":"Rabat"}})
  81. // answer: it will create the field
  82.  
  83. // And show the field can now be updated with the same command
  84. db.iWantToGoToThere.update({"country": "Morocco"}, {$set: {"capital":"RABAT"}})
  85.  
  86. // Show how to push to an array with $push
  87. db.iWantToGoToThere.update({"country": "Morocco"}, {$push: {"majorcities":"Agadir"}})
  88.  
  89. // Show how to delete an entry with db.[COLLECTION_NAME].remove()
  90. db.iWantToGoToThere.remove({"country":"Morocco"})
  91.  
  92. // Show how to empty a collection with db.[COLLECTION_NAME].remove()
  93. db.iWantToGoToThere.remove({})
  94.  
  95. // Show how to drop a collection with db.[COLLECTION_NAME].drop()
  96. db.iWantToGoToThere.drop()
  97.  
  98. // Show how to drop a database
  99. db.dropDatabase()
  100.  
  101. db.classroom.remove({name: [name of another neighbor]})
  102.  
  103. db.classroom.update({}, {$set: {gavecandy:false}}, {multi:true})
  104.  
  105. db.classroom.update({name:'Steve'}, {$set: {gavecandy:true}})
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top