Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------------------------------------------------------------------
- 4)
- //TEGO NIE TRZEBA, samo wstawienie do kolekcji tworzy tabelę w bazie
- db.createCollection("students", {
- validator: {
- SjsonSchema: {
- bsonType: "object",
- required: ["imie", "mazwisko", "obecnosc", "aktualna_data", "zaliczone_przedmioty" ],
- properties: {
- imie: {
- bsonType: "string",
- description: "must be a string and is required"
- },
- nazwisko: {
- bsonType: "string",
- description: "must be a string and is required"
- },
- obecnosc: {
- bsonType: "bool",
- description: "must be true of false"
- },
- aktualna_data: {
- bsonType: "date",
- description: "must be valid date format"
- },
- zaliczone_przedmioty: {
- bsonType: "string",
- description: "must be a valid subject"
- }
- }
- }
- }
- })
- //TO JUŻ TRZEBA:
- db.students.insert({
- imie: "Jan",
- nazwisko: "Nowak",
- obeconsc: true,
- ocena_z_lab: null,
- aktualna_data: ISODate("2019-11-19"),
- zaliczone_przedmioty: ["Math", "Science", "PE"]
- })
- db.students.insert({
- imie: "John",
- nazwisko: "Newman",
- obecnosc: true,
- ocena_z_lab: 4.5,
- aktualna_data: ISODate("2019-11-20"),
- zaliczone_przedmioty: ["Math"]
- })
- db.students.insert({
- imie: "John",
- nazwisko: "Smith",
- obecnosc: true,
- ocena_z_lab: 4.0,
- aktualna_data: ISODate("2019-11-20"),
- zaliczone_przedmioty: ["PE"]
- })
- ---------------------------------------------------------------------------------------------------------------------------
- 5)
- 5.1)
- db.business.aggregate(
- [
- {
- $match: {
- stars: {
- $eq: 5.0
- }
- }
- },
- {
- $count: "five_star_places:"
- }
- ]
- )
- albo
- db.business.count({stars:5.0})
- 5.2)
- db.business.aggregate(
- [
- { $match: { categories: "Restaurants" } },
- { $group: { _id: "$city", totalRestaurants: { $sum: 1} }},
- { $sort: { totalRestaurants: -1}}
- ]
- )
- ---------------------------------------------------------------------------------------------------------------------------
- 5.3)
- db.business.aggregate(
- [
- { $match: {'categories': "Hotels & Travel", attributes: { 'Wi-Fi': 'free'}, stars: {$gte: 4.5}}},
- { $group: { _id: "$state", totalRestaurants: { $sum: 1} }}
- ]
- )
- ---------------------------------------------------------------------------------------------------------------------------
- 5.4)
- import com.mongodb.*;
- import java.net.UnknownHostException;
- import java.text.Collator;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- public class MongoLab {
- private MongoClient mongoClient;
- private DB db;
- public MongoLab() throws UnknownHostException {
- mongoClient = new MongoClient();
- db = mongoClient.getDB("MichalGrzybek01");
- }
- private void showCollection(){
- for ( String name : db.getCollectionNames()){
- System.out.println("colname: " + name);
- }
- }
- private void number_of_places(){
- //a
- DBCollection business = db.getCollection("business");
- DBCursor dbObjects = business.find();
- int counter = 0;
- while (dbObjects.hasNext()){
- DBObject object = dbObjects.next();
- Object stars = object.get("stars");
- if (stars.toString().equals("5.0")){
- counter ++;
- }
- }
- System.out.println(counter);
- }
- private void number_of_restaurants(){
- //b
- DBCollection business = db.getCollection("business");
- DBCursor dbObjects = business.find();
- Map<String, Integer> cities_restaurants = new HashMap<String, Integer>();
- while(dbObjects.hasNext()){
- DBObject object = dbObjects.next();
- String city = (String)object.get("city").toString();
- if (! cities_restaurants.keySet().contains(city)){
- cities_restaurants.put(city, 0);
- }
- BasicDBList categories = (BasicDBList)object.get("categories");
- if (categories.contains("Restaurants")){
- cities_restaurants.put(city, cities_restaurants.get(city) + 1);
- }
- }
- cities_restaurants.entrySet().stream()
- .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
- .forEach(System.out::println);
- }
- private void number_of_hotels(){
- //c
- DBCollection business = db.getCollection("business");
- DBCursor dbObjects = business.find();
- Map<String, Integer> states_hotels = new HashMap<String, Integer>();
- while(dbObjects.hasNext()){
- DBObject object = dbObjects.next();
- String state = (String)object.get("state").toString();
- if (! states_hotels.keySet().contains(state)){
- states_hotels.put(state, 0);
- }
- BasicDBObject attributes = (BasicDBObject)object.get("attributes");
- BasicDBList categories = (BasicDBList)object.get("categories");
- if ((attributes.containsField("Wi-Fi")
- && (attributes.get("Wi-Fi").toString().equals("free"))
- && categories.contains("Hotels & Travel"))){
- states_hotels.put(state, states_hotels.get(state) + 1);
- }
- }
- states_hotels.entrySet().stream()
- .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
- .forEach(System.out::println);
- }
- private void reviews_per_category(){
- //d
- DBCollection review = db.getCollection("review");
- DBCursor dbObjects = review.find();
- Map<String, Integer> categories_count = new HashMap<String, Integer>();
- categories_count.put("funny", 0);
- categories_count.put("useful", 0);
- categories_count.put("cool", 0);
- while(dbObjects.hasNext()){
- DBObject object = dbObjects.next();
- DBObject categories = (DBObject)object.get("votes");
- int funny = Integer.valueOf(categories.get("funny").toString());
- int useful = Integer.valueOf(categories.get("useful").toString());
- int cool = Integer.valueOf(categories.get("cool").toString());
- if (funny > 0){
- categories_count.put("funny", categories_count.get("funny") + 1);
- }
- if (useful > 0){
- categories_count.put("useful", categories_count.get("useful") + 1);
- }
- if (cool > 0){
- categories_count.put("cool", categories_count.get("cool") + 1);
- }
- }
- for (String key: categories_count.keySet()){
- System.out.println(key + " " + String.valueOf(categories_count.get(key)));
- }
- }
- private void most_friendly_user(){
- DBCollection review = db.getCollection("review");
- DBCursor dbObjects = review.find();
- Map<String, Integer> user_reviews = new HashMap<String, Integer>();
- while(dbObjects.hasNext()){
- DBObject object = dbObjects.next();
- String user_id = object.get("user_id").toString();
- if (! user_reviews.keySet().contains(user_id)){
- user_reviews.put
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement