Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. This Tutorial Shows you some MongoDB Command Line -
  2.  
  3.  
  4. 1. use yt
  5.  
  6. 2. $ db.testData.insert({"name" : "Stanley Kubrick"});
  7.  
  8. 3. $ db.testData.insert([{"name" : "Arthur C. Clarke"}, {"name" : "Ray Lovejoy"}]);
  9.  
  10. 4. $ db.testData.find().pretty();
  11.  
  12.  
  13. 5. $ db.testData.update({"name" : "Stanley Kubrick"}, {$set:{"name" : "Stanley Kubrick Update", "New Update of the Name" : 04}});
  14.  
  15. 6. $ db.testData.find().pretty();
  16.  
  17.  
  18. 7. $ db.testData.insert([{"name" : "Stanley Kubrick", "Space Odyssey" : "2001"}, {"name" : "Arthur C. Clarke", "Space Odyssey" : "2001"}, {"name" : "Ray Lovejoy", "Space Odyssey" : "2001"}]);
  19.  
  20.  
  21. 8. $ db.testData.update({"Space Odyssey" : "2001"}, {$set:{"Space Odyssey" : "2001 in New York"}}, {multi:true});
  22.  
  23.  
  24. 9. $ db.testData.find().pretty();
  25.  
  26.  
  27. 10. $ db.testData.find({"name" : "Stanley Kubrick"});
  28.  
  29.  
  30. 11. $ db.testData.update({"name" : "Stanley Kubrick"}, {"$inc" : {"2001" : 1}});
  31.  
  32.  
  33. 12. $ db.testData.find().pretty();
  34.  
  35. 13. $ db.testData.update({"name" : "Stanley Kubrick"}, {"$inc" : {"2001" : 2002}});
  36.  
  37.  
  38. 14. $ db.testData.find().pretty();
  39.  
  40.  
  41. 15. $ db.testData.update({"name" : "Ray Lovejoy"}, {"$push" : {"Movie" : {"Science Fiction" : 1968, "Film Director" : 2001, "Budget" : 1000000}}});
  42.  
  43. 16. $ db.testData.find({"name" : "Ray Lovejoy"}).pretty();
  44.  
  45.  
  46. 17. $ db.testData.update({"name" : "Ray Lovejoy"}, {"$push" : {"Best Science Fiction Movie all the Time" : {"$each" : [1968, 2001, 2017, 2050], "$slice" : -5}}});
  47.  
  48.  
  49. 18. $ db.testData.find({"name" : "Ray Lovejoy"}).pretty();
  50.  
  51. 19. $ db.testData.update({"name" : "Ray Lovejoy"}, {"$addToSet" : {"Best Movie all the Time WorldWide" : 47}});
  52.  
  53.  
  54. 20. $ db.testData.find({"name" : "Ray Lovejoy"}).pretty();
  55.  
  56. 21. $ db.testData.update({"name" : "Ray Lovejoy"}, {"$addToSet" : {"Best Science Fiction Movie all the Time" : 2047}});
  57.  
  58. 22. $ db.testData.find({"name" : "Ray Lovejoy"}).pretty();
  59.  
  60. 23. $ db.testData.update({"name" : "Ray Lovejoy"}, {"$pop" : {"Best Science Fiction Movie all the Time" : 7}});
  61.  
  62. 24. $ db.testData.find({"name" : "Ray Lovejoy"}).pretty();
  63.  
  64. 25. $   
  65.  
  66. 26. $ db.testData.find({"name" : "Ray Lovejoy"}).pretty();
  67.  
  68. 27. $ db.testData.update({"name" : "Ray Lovejoy"}, {"$pull" : {"Best Science Fiction Movie all the Time" : 2001}});
  69.  
  70. 28. $ db.testData.find({"name" : "Ray Lovejoy"}).pretty();
  71.  
  72. 29. $
  73.  
  74.  
  75.  
  76.  
  77. 30. Insert Data to the MongoDB -
  78.  
  79. $ db.testData.insert([{"name" : "Lisbeth Salander", "street" : "123 Main St", "city" : "St Gallen", "state" : "SG", "dob" : new Date(1959, 2, 22), "sex" : "M", "gpa" : 3.5, "status" : "student", "tests" : [10, 9, 8], "contact info" : {"email" : "ls@gmail.com", "phone" : "792-223-8901"}},
  80. {"name" : "Lucas Gatsas", "street" : "Neugasse 1", "city" : "St Gallen", "state" : "SG", "dob" : new Date(1946, 1, 24), "sex" : "M", "gpa" : 3.4, "status" : "student", "tests" : [8, 9, 8], "contact info" : {"email" : "ht@gmail.com", "phone" : "792-223-9810"}},
  81. {"name" : "Hans Huebner", "street" : "9 Pond Rd", "city" : "Sparks", "state" : "NV", "dob" : new Date(1970, 12, 12), "sex" : "F", "gpa" : 2.9, "status" : "dropout", "tests" : [10, 8, 0], "contact info" : {"email" : "hh@gmail.com", "phone" : "792-223-6734"}},
  82. {"name" : "Paul Ziegler", "street" : "14 12th St", "city" : "Bern", "state" : "CA", "dob" : new Date(1967, 5, 24), "sex" : "M", "gpa" : 2.0, "status" : "student", "tests" : [5, 4, 6], "contact info" : {"email" : "pz@aol.com", "phone" : "792-223-6178"}},
  83. {"name" : "Lucy Hackbash", "street" : "120 16th St", "city" : "St Gallen", "state" : "IA", "dob" : new Date(1970, 3, 24), "sex" : "F", "gpa" : 3.7, "status" : "student", "tests" : [10, 8, 8], "contact info" : {"email" : "l@gmail.com", "phone" : "792-223-2001"}},
  84. {"name" : "Charlotte Hings", "street" : "342 19th St", "city" : "Zuerich", "state" : "MI", "dob" : new Date(1965, 2, 1), "sex" : "F", "gpa" : 3.0, "status" : "student", "tests" : [9, 9, 8], "contact info" : {"email" : "ah@gmail.com", "phone" : "792-223-2002"}},
  85. ])
  86.  
  87.  
  88. // BLOCK TWO with FAKE USERS.
  89.  
  90.  
  91. $ db.testData.insert([{"name" : "Lucas Gatsas", "street" : "Mainstrasse 72", "city" : "Zuerich", "state" : "ZH", "dob" : new Date(1959, 2, 22), "sex" : "M", "gpa" : 3.5, "status" : "student", "tests" : [10, 9, 8], "contact info" : {"email" : "ls@gmail.com", "phone" : "792-223-8901"}},
  92. {"name" : "Steven Stone", "street" : "Schwertstrasse 1", "city" : "Zuerich", "state" : "ZH", "dob" : new Date(1946, 1, 24), "sex" : "M", "gpa" : 3.4, "status" : "student", "tests" : [8, 9, 8], "contact info" : {"email" : "steven@gmail.com", "phone" : "792-223-9810"}},
  93. {"name" : "Diana Bash", "street" : "Fahrstrasse 1", "city" : "Sparks", "state" : "NV", "dob" : new Date(1970, 12, 12), "sex" : "F", "gpa" : 2.9, "status" : "dropout", "tests" : [10, 8, 0], "contact info" : {"email" : "hh@gmail.com", "phone" : "792-223-6734"}},
  94. {"name" : "Jade Himmelstern", "street" : "Multerstrasse 3", "city" : "St Gallen", "state" : "SG", "dob" : new Date(1967, 5, 24), "sex" : "M", "gpa" : 2.0, "status" : "student", "tests" : [5, 4, 6], "contact info" : {"email" : "pz@aol.com", "phone" : "792-223-6178"}},
  95. {"name" : "Beatrix Mueller", "street" : "Neuestrasse 18", "city" : "St Gallen", "state" : "SG", "dob" : new Date(1970, 3, 24), "sex" : "F", "gpa" : 3.7, "status" : "student", "tests" : [10, 8, 8], "contact info" : {"email" : "Beatrix@gmail.com", "phone" : "792-223-2001"}},
  96. {"name" : "Mari Kuster", "street" : "Teufnerstrasse 152", "city" : "Bern", "state" : "BE", "dob" : new Date(1965, 2, 1), "sex" : "F", "gpa" : 3.0, "status" : "student", "tests" : [9, 9, 8], "contact info" : {"email" : "marh@gmail.com", "phone" : "792-223-2002"}},
  97. ])
  98.  
  99. $ db.testData.find().pretty();
  100.  
  101. 31. $ db.testData.find({"state" : "ZH", "sex" : "M"}).pretty();
  102.  
  103. $ db.testData.find().pretty();
  104.  
  105. 32. $ db.testData.find({"state" : "ZH", "sex" : "M"}, {"name" : 1, "dob" : 1}).pretty();
  106.  
  107. $ db.testData.find().pretty();
  108.  
  109.  
  110. 34. $ db.testData.find({"gpa" : {"$gte" : 3.0, "$lte" : 3.5}}, {"name" : 1, "gpa" : 1, "_id" : 0});
  111.  
  112. $ db.testData.find().pretty();
  113.  
  114.  
  115. 35. $ year1969 = new Date("01/01/1969");
  116.  
  117. $ db.testData.find().pretty();
  118.  
  119.  
  120. $ db.testData.find().pretty();
  121.  
  122. 37. $ db.testData.find({"sex" : {"$ne" : "M"}, "status" : "student"}, {"name" : 1, "_id" : 0});
  123.  
  124. $ db.testData.find().pretty();
  125.  
  126. 38. $ db.testData.find({"gpa" : {"$in" : [3.4, 3.5, 3.6, 3.7]}}, {"name" : 1, "_id" : 0});
  127.  
  128. $ db.testData.find().pretty();
  129.  
  130. 39. $ db.testData.find({"gpa" : {"$nin" : [3.4, 3.5, 3.6, 3.7]}}, {"name" : 1, "_id" : 0});
  131.  
  132. $ db.testData.find().pretty();
  133.  
  134. 40. $ db.testData.find({"$or" : [{"status" : "dropout"}, {"gpa" : {"$lt" : 3.0}}]}, {"name" : 1, "_id" : 0});
  135.  
  136. $ db.testData.find().pretty();
  137.  
  138. 41. $ db
  139.  
  140. $ db.testData.find().pretty();
  141.  
  142. 42. $ db.testData.find({"gpa" : {$not : {$gt : 3.0}}}, {"name" : 1, "_id" : 0}).limit(3);
  143.  
  144. $ db.testData.find().pretty();
  145.  
  146. 43. $ db.testData.find({"gpa" : {$not : {$gt : 3.0}}}, {"name" : 1, "_id" : 0}).skip(3);
  147.  
  148. $ db.testData.find().pretty();
  149.  
  150. 44. $ db.testData.find({"gpa" : {$not : {$gt : 3.0}}}, {"name" : 1, "_id" : 0}).sort({"nam" : 1});
  151.  
  152. $ db.testData.find().pretty();
  153.  
  154. 45. $ db.testData.find({"name" : /^D.*$/}, {"name" : 1, "_id" :0});
  155.  
  156. $ db.testData.find().pretty();
  157.  
  158. 46. $ db.testData.find({"tests" : 0}, {"name" : 1, "_id" : 0});
  159.  
  160. 47. $ db.testData.find().pretty();
  161.  
  162. 48. $ db.testData.find({"tests.0" : 10}, {"name" : 1, "_id" : 0})
  163.  
  164. $ db.testData.find().pretty();
  165.  
  166. 49. $ db.testData.find({"tests" : {$all : [0, 10]}}, {"name" : 1, "_id" : 0});
  167.  
  168. $ db.testData.find().pretty();
  169.  
  170. 50. $ db.testData.update({"name" : "Lucas Gatsas"}, {"$push" : {"tests" : 9}});
  171.  
  172. $ db.testData.find().pretty();
  173.  
  174.  
  175. 52. $ db.testData.find({"name" : "Lucas Gatsas"}, {"test" : {"$slice" : 2}});
  176.  
  177. $ db.testData.find().pretty();
  178.  
  179. 53. $ db.testData.find({"name" : "Lucas Gatsas"}, {"test" : {"$slice" : -2}}).pretty();
  180.  
  181. $ db.testData.find().pretty();
  182.  
  183.  
  184. 54. $ db.testData.find({"name" : "Lucas Gatsas"}, {"test" : {"$slice" : [1,2]}}).pretty();
  185.  
  186. $ db.testData.find().pretty();
  187.  
  188. 55. $ db.testData.find({"name" : "Lucas Gatsas"}, {"test" : {"$slice" : -2}}).pretty();
  189.  
  190. $ db.testData.find().pretty();
  191.  
  192. 56. $ db.testData.find({"name" : "Lucas Gatsas"}, {"test" : {"$slice" : [1,2]}}).pretty();
  193.  
  194. $ db.testData.find().pretty();
  195.  
  196. 57. $ db.testData.find({"name" : "Lisbeth Salander"}, {"name" : 1, "contact info.email" : 1, "_id" : 0});
  197.  
  198. 58. $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement