Advertisement
Guest User

Untitled

a guest
Oct 25th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. db.User.insert([
  2. {
  3. _id: 100,
  4. fname: "John",
  5. surname: "Smith",
  6. age: 35,
  7. email: "jsmith@gmail.com",
  8. sex: "M",
  9. title:"Mr",
  10. car: {
  11. carReg: "131-G-101",
  12. fuel: "petrol"
  13. }
  14. },
  15. {
  16. _id: 101,
  17. fname: "Sean",
  18. surname: "Murphy",
  19. age: 22,
  20. email: "seanmurphy@yahoo.com",
  21. sex: "M",
  22. title:"Mr",
  23. car: {
  24. carReg: "172-G-200",
  25. electric: "true",
  26. }
  27. },
  28. {
  29. _id: 102,
  30. fname: "Aine",
  31. surname: "Browne",
  32. age: 23,
  33. email: "abrowne@gmail.com",
  34. sex: "F",
  35. title:"Ms"
  36. },
  37. {
  38. _id: 103,
  39. fname: "Alan",
  40. surname: "Murphy",
  41. age: 25,
  42. email: "murpha@hotmail.com",
  43. sex: "M"
  44. },
  45. {
  46. _id: 104,
  47. fname: "Sarah",
  48. surname: "Doyle",
  49. age: 24,
  50. email: "sarah@gmail.com",
  51. sex: "F",
  52. car: {
  53. carReg: "141-MO-123",
  54. fuel: "diesel",
  55. }
  56. },
  57. {
  58. _id: 105,
  59. fname: "Bill",
  60. surname: "Mulligan",
  61. age: 20,
  62. email: "billy123@gmail.com",
  63. sex: "M",
  64. car: {
  65. carReg: "12-RN-445",
  66. fuel: "petrol",
  67. }
  68. },
  69. {
  70. _id: 106,
  71. fname: "Shane",
  72. surname: "Kelly",
  73. age: 25,
  74. email: "sk998@yahoo.com",
  75. sex: "M",
  76. title : "Mr",
  77. car: {
  78. carReg: "11-WH-7783",
  79. fuel: "petrol",
  80. }
  81. },
  82. {
  83. _id: 107,
  84. fname: "Will",
  85. surname: "Doyle",
  86. age: 20,
  87. email: "doyler123@gmail.com",
  88. sex: "M",
  89. title : "Mr"
  90. }
  91. ]);
  92.  
  93. //age 19
  94. db.User.find({age: 19})
  95.  
  96. //greater than 19
  97. db.User.find({age: {$gt: 19}})
  98.  
  99. //greater than 19 & has car
  100. db.User.find({age: {$gt: 19}, carReg: {$exists:true}})
  101.  
  102. //greater than 1105 & age > 20
  103. db.User.find({_id: {$gt: 104}, age: {$gt: 20}})
  104.  
  105. //greater than 19 & has car
  106. db.User.findOne({ carReg: {$exists:true}})
  107.  
  108. /*9. User 106 - Shane has bought a car with reg 12-G-1234. The following command was run to update the user’s document:
  109. db.users.save({_id:106, carReg:"12-G-1234"})
  110. What does the document look like now and why?
  111. */ -->Bill now has a car reg added to obj
  112.  
  113. /*10. User 102 - Sean has bought a car with reg 10-G-9876. The following command was run to update the user’s document:
  114. db.user.update({_id:102}, {carReg:"10-G-9876"})
  115. What does the document look like now and why?
  116. *? */--> over writtes record to contain only id & carReg
  117.  
  118. /*11. User 105 – Bill’s document is as follows:
  119. { "_id" : 105, "fname" : "Bill", "surname" : "Mulligan", "age" : 19, "email" : "billy123@gmail.com" }
  120. Bill has bought a car with reg 161-MO-4. Give the command so that Bill’s document now looks as follows:
  121. db.Users.update{ "_id" : 105, "fname" : "Bill", "surname" : "Mulligan", "age" : 19, "email" : "billy123@gmail.com", "carReg" : "161-MO-4" }
  122.  
  123. */
  124. db.User.update({_id: 106},{carReg : "161-MO-4" })
  125.  
  126. /*12. User 106’s document now looks as follows:
  127. { "_id" : 106, "carReg" : "12-G-1234" }
  128. */
  129. db.User.save({
  130. _id: 107,
  131. fname: "Shane",
  132. surname: "Kelly",
  133. age: 24,
  134. email: "sk998@yahoo.com",
  135. carReg: "12-G-1234"
  136. })
  137.  
  138. //13. Give the mongodb command to add 1 to each user’s age.
  139. db.User.update({age: {$exists:true}},{$inc:{age:1}},{multi:true})
  140.  
  141. db.User.update([
  142. {{ _id: 100},{$set :{sex:"M"}}},
  143. {{ _id: 101},{$set :{sex:"M"}}},
  144. {{ _id: 102},{$set :{sex:"M"}}},
  145. {{ _id: 103},{$set :{sex:"M"}}},
  146. {{ _id: 104},{$set :{sex:"F"}}},
  147. {{ _id: 105},{$set :{sex:"M"}}},
  148. {{ _id: 106},{$set :{sex:"M"}}},
  149. {{ _id: 107},{$set :{sex:"M"}}}
  150. ])
  151.  
  152. db.User.updateMany([
  153. { _id: 101},{$set :{sex:"M"}},
  154. { _id: 102},{$set :{sex:"M"}},
  155. { _id: 103},{$set :{sex:"M"}},
  156. { _id: 104},{$set :{sex:"F"}},
  157. { _id: 105},{$set :{sex:"M"}},
  158. { _id: 106},{$set :{sex:"M"}},
  159. { _id: 107},{$set :{sex:"M"}}
  160. ])
  161.  
  162.  
  163. //16.Users 101 – Sean, 103 – Alan and 107 – Will have sold their cars, update the
  164. db.User.update({_id: 101},{$unset:{carReg:0}})
  165. db.User.update({_id: 107},{$unset:{carReg:0}})
  166.  
  167. //17.Give the mongodb command to list/show only the fname, surname,
  168. // age and sex attributes of documents where the _id is between 101 and 107 inclusive.
  169. db.User.find({_id: {$gt: 104}, age: {$gt: 20}})
  170.  
  171.  
  172. ///lab2:
  173.  
  174. //show indices
  175. db.User.getIndexes()
  176.  
  177. //create index on age
  178. db.User.createIndex({age: 1})
  179.  
  180. //4.Give the mongodb
  181. // command to create a descending index on the surname attribute
  182. db.User.createIndex({surname: -1})
  183.  
  184. //5. Give the mongodb command to create
  185. // a descending index on the age attribute.
  186. db.User.createIndex({age: -1})
  187.  
  188. //6. Give the mongodb command to remove
  189. // the ascending index on the age attribute.
  190. db.User.dropIndex({age: 1})
  191.  
  192. //7. Give the mongodb command to show only the fname and
  193. //surname attributes of users who don’t have a car.
  194. db.User.find({car: {$exists:false}},{_id: 0,fname: 1,surname:1})
  195.  
  196.  
  197. // 8.Give the mongodb command to show only the fname,
  198. // surname, reg and fuel attributes of users who have a car.
  199. db.User.find({car: {$exists:true}},
  200. {_id: 0, fname: 1, surname:1, "car.fuel" : 1})
  201.  
  202.  
  203. //9.Give the mongodb command to list details of all users
  204. // (except for their car). The list should be in sorted by name.
  205. db.User.find({_id: {$exists:true}},{"car": 0}).sort({fname: 1})
  206.  
  207. //10.Give the mongodb command to list only details of all cars.
  208. // The list should be in sorted by car registration.
  209. db.User.find({car: {$exists:true}}, {_id: 0,fname: 0,surname:0, age:0, email: 0, sex: 0, title:0}).sort({"car.carReg": 1})
  210.  
  211. //11.Give the mongodb command to list the documents that have an
  212. //electric attribute in the car attribute.
  213. db.User.find({"car.electric": {$exists:true}})
  214.  
  215. //12.Give the mongodb command to list only the fname and surname
  216. // attributes of users who have a petrol car.
  217. db.User.find({"car.fuel":"petrol"}, {_id: 0,fname: 1,surname:2})
  218.  
  219. //14. Give the mongodb command to delete all females between the ages
  220. //of 21 and 24 inclusive.
  221. db.User.deleteMany({age:{$gt:20, $lt: 25}})
  222.  
  223. //15.Give the mongodb command to delete the fuel attribute from all documents.
  224. //db.User.deleteMany({car.fuel:{"petrol"})
  225. db.User.update({car: {$exists:true}},
  226. {$unset: {"car.fuel":1}},
  227. {multi: true})
  228.  
  229. //16. Give the mongodb command to give everyone who owns a car the title of Owner.
  230. db.User.update({car: {$exists:true}},
  231. {$set: {title: "Owner"}},
  232. {multi: true})
  233.  
  234. //17.Give the mongodb command to increase the age of all Males who own a car by 10 years.
  235. db.User.update({car: {$exists:true}},
  236. {$inc: {age: 10}},
  237. {multi: true})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement