Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. ---------------------------------------------------------------------------------------------------------------------------
  2. 4)
  3.  
  4. //TEGO NIE TRZEBA, samo wstawienie do kolekcji tworzy tabelę w bazie
  5. db.createCollection("students", {
  6. validator: {
  7. SjsonSchema: {
  8. bsonType: "object",
  9. required: ["imie", "mazwisko", "obecnosc", "aktualna_data", "zaliczone_przedmioty" ],
  10. properties: {
  11. imie: {
  12. bsonType: "string",
  13. description: "must be a string and is required"
  14. },
  15. nazwisko: {
  16. bsonType: "string",
  17. description: "must be a string and is required"
  18. },
  19. obecnosc: {
  20. bsonType: "bool",
  21. description: "must be true of false"
  22. },
  23. aktualna_data: {
  24. bsonType: "date",
  25. description: "must be valid date format"
  26. },
  27. zaliczone_przedmioty: {
  28. bsonType: "string",
  29. description: "must be a valid subject"
  30. }
  31. }
  32. }
  33. }
  34. })
  35.  
  36. //TO JUŻ TRZEBA:
  37. db.students.insert({
  38. imie: "Jan",
  39. nazwisko: "Nowak",
  40. obeconsc: true,
  41. ocena_z_lab: null,
  42. aktualna_data: ISODate("2019-11-19"),
  43. zaliczone_przedmioty: ["Math", "Science", "PE"]
  44. })
  45.  
  46.  
  47. db.students.insert({
  48. imie: "John",
  49. nazwisko: "Newman",
  50. obecnosc: true,
  51. ocena_z_lab: 4.5,
  52. aktualna_data: ISODate("2019-11-20"),
  53. zaliczone_przedmioty: ["Math"]
  54. })
  55.  
  56. db.students.insert({
  57. imie: "John",
  58. nazwisko: "Smith",
  59. obecnosc: true,
  60. ocena_z_lab: 4.0,
  61. aktualna_data: ISODate("2019-11-20"),
  62. zaliczone_przedmioty: ["PE"]
  63. })
  64.  
  65.  
  66. ---------------------------------------------------------------------------------------------------------------------------
  67. 5)
  68.  
  69. 5.1)
  70. db.business.aggregate(
  71. [
  72. {
  73. $match: {
  74. stars: {
  75. $eq: 5.0
  76. }
  77. }
  78. },
  79. {
  80. $count: "five_star_places:"
  81. }
  82. ]
  83. )
  84.  
  85. albo
  86.  
  87. db.business.count({stars:5.0})
  88.  
  89. 5.2)
  90.  
  91. db.business.aggregate(
  92. [
  93. { $match: { categories: "Restaurants" } },
  94. { $group: { _id: "$city", totalRestaurants: { $sum: 1} }},
  95. { $sort: { totalRestaurants: -1}}
  96. ]
  97. )
  98.  
  99. ---------------------------------------------------------------------------------------------------------------------------
  100. 5.3)
  101.  
  102. db.business.aggregate(
  103. [
  104. { $match: {'categories': "Hotels & Travel", attributes: { 'Wi-Fi': 'free'}, stars: {$gte: 4.5}}},
  105. { $group: { _id: "$state", totalRestaurants: { $sum: 1} }}
  106. ]
  107. )
  108.  
  109. ---------------------------------------------------------------------------------------------------------------------------
  110. 5.4)
  111.  
  112.  
  113.  
  114. import com.mongodb.*;
  115.  
  116. import java.net.UnknownHostException;
  117. import java.text.Collator;
  118. import java.util.*;
  119. import java.util.stream.Collectors;
  120. import java.util.stream.Stream;
  121.  
  122. public class MongoLab {
  123. private MongoClient mongoClient;
  124. private DB db;
  125.  
  126. public MongoLab() throws UnknownHostException {
  127. mongoClient = new MongoClient();
  128. db = mongoClient.getDB("MichalGrzybek01");
  129. }
  130.  
  131. private void showCollection(){
  132. for ( String name : db.getCollectionNames()){
  133. System.out.println("colname: " + name);
  134. }
  135. }
  136. private void number_of_places(){
  137. //a
  138. DBCollection business = db.getCollection("business");
  139. DBCursor dbObjects = business.find();
  140.  
  141. int counter = 0;
  142.  
  143. while (dbObjects.hasNext()){
  144. DBObject object = dbObjects.next();
  145. Object stars = object.get("stars");
  146. if (stars.toString().equals("5.0")){
  147. counter ++;
  148. }
  149.  
  150. }
  151. System.out.println(counter);
  152. }
  153.  
  154. private void number_of_restaurants(){
  155. //b
  156. DBCollection business = db.getCollection("business");
  157. DBCursor dbObjects = business.find();
  158.  
  159. Map<String, Integer> cities_restaurants = new HashMap<String, Integer>();
  160.  
  161. while(dbObjects.hasNext()){
  162. DBObject object = dbObjects.next();
  163.  
  164. String city = (String)object.get("city").toString();
  165.  
  166. if (! cities_restaurants.keySet().contains(city)){
  167. cities_restaurants.put(city, 0);
  168. }
  169.  
  170. BasicDBList categories = (BasicDBList)object.get("categories");
  171. if (categories.contains("Restaurants")){
  172. cities_restaurants.put(city, cities_restaurants.get(city) + 1);
  173. }
  174. }
  175.  
  176.  
  177. cities_restaurants.entrySet().stream()
  178. .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
  179. .forEach(System.out::println);
  180.  
  181. }
  182.  
  183. private void number_of_hotels(){
  184. //c
  185. DBCollection business = db.getCollection("business");
  186. DBCursor dbObjects = business.find();
  187.  
  188. Map<String, Integer> states_hotels = new HashMap<String, Integer>();
  189.  
  190. while(dbObjects.hasNext()){
  191. DBObject object = dbObjects.next();
  192.  
  193. String state = (String)object.get("state").toString();
  194.  
  195. if (! states_hotels.keySet().contains(state)){
  196. states_hotels.put(state, 0);
  197. }
  198.  
  199. BasicDBObject attributes = (BasicDBObject)object.get("attributes");
  200. BasicDBList categories = (BasicDBList)object.get("categories");
  201.  
  202. if ((attributes.containsField("Wi-Fi")
  203. && (attributes.get("Wi-Fi").toString().equals("free"))
  204. && categories.contains("Hotels & Travel"))){
  205.  
  206. states_hotels.put(state, states_hotels.get(state) + 1);
  207. }
  208.  
  209. }
  210.  
  211.  
  212. states_hotels.entrySet().stream()
  213. .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
  214. .forEach(System.out::println);
  215.  
  216. }
  217.  
  218. private void reviews_per_category(){
  219. //d
  220. DBCollection review = db.getCollection("review");
  221. DBCursor dbObjects = review.find();
  222.  
  223. Map<String, Integer> categories_count = new HashMap<String, Integer>();
  224. categories_count.put("funny", 0);
  225. categories_count.put("useful", 0);
  226. categories_count.put("cool", 0);
  227.  
  228. while(dbObjects.hasNext()){
  229. DBObject object = dbObjects.next();
  230.  
  231. DBObject categories = (DBObject)object.get("votes");
  232.  
  233. int funny = Integer.valueOf(categories.get("funny").toString());
  234. int useful = Integer.valueOf(categories.get("useful").toString());
  235. int cool = Integer.valueOf(categories.get("cool").toString());
  236.  
  237. if (funny > 0){
  238. categories_count.put("funny", categories_count.get("funny") + 1);
  239. }
  240. if (useful > 0){
  241. categories_count.put("useful", categories_count.get("useful") + 1);
  242. }
  243. if (cool > 0){
  244. categories_count.put("cool", categories_count.get("cool") + 1);
  245. }
  246. }
  247.  
  248. for (String key: categories_count.keySet()){
  249. System.out.println(key + " " + String.valueOf(categories_count.get(key)));
  250. }
  251.  
  252. }
  253. private void most_friendly_user(){
  254. DBCollection review = db.getCollection("review");
  255. DBCursor dbObjects = review.find();
  256.  
  257. Map<String, Integer> user_reviews = new HashMap<String, Integer>();
  258.  
  259. while(dbObjects.hasNext()){
  260. DBObject object = dbObjects.next();
  261.  
  262. String user_id = object.get("user_id").toString();
  263.  
  264. if (! user_reviews.keySet().contains(user_id)){
  265. user_reviews.put
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement