Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:cn.edu.hfut.dmic.webcollector.example.WeiboCrawler.java

License:Open Source License

@Override
public void visit(Page page, CrawlDatums next) {
    int pageNum = Integer.valueOf(page.getMetaData("pageNum"));
    /*??*///from w w  w  .  j  a  v  a2s .  c om
    Elements weibos = page.select("div.c");
    try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);

        // ?
        MongoDatabase mongoDatabase = mongoClient.getDatabase("weibo_crawler");
        System.out.println("Connect to database successfully");
        MongoCollection<Document> collection = mongoDatabase.getCollection("webpage");
        //?  
        /** 
        * 1.  org.bson.Document ?key-value? 
        * 2. ?List<Document> 
        * 3. ???? mongoCollection.insertMany(List<Document>) ??? mongoCollection.insertOne(Document) 
        * */
        for (Element weibo : weibos) {
            Document document = new Document("content", "" + pageNum + "" + ":" + weibo.text());
            List<Document> documents = new ArrayList<Document>();
            documents.add(document);
            collection.insertMany(documents);
        }
        System.out.println("??");
        mongoClient.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:cn.weibo.webcollector.spider.WeiboCrawler.java

License:Open Source License

@Override
public void visit(Page page, CrawlDatums next) {
    String inlink = page.meta("inlink");
    String title = page.select("title").text();
    String url = page.getUrl();// w  w w . ja va2s  .  co m
    /*??*/
    Elements weibos = page.select("div[id].c");
    try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
        mongoLogger.setLevel(Level.ERROR);

        // ?
        MongoDatabase mongoDatabase = mongoClient.getDatabase("weibo_crawler");
        System.out.println("Connect to database successfully");
        MongoCollection<Document> collection = mongoDatabase.getCollection("weibo_page");
        //?  
        /** 
        * 1.  org.bson.Document ?key-value? 
        * 2. ?List<Document> 
        * 3. ???? mongoCollection.insertMany(List<Document>) ??? mongoCollection.insertOne(Document) 
        * */
        for (Element weibo : weibos) {
            if (weibo.text().length() != 0) {
                Document document = new Document("url", url).append("title", title)
                        .append("content", weibo.text()).append("inlink", inlink);
                List<Document> documents = new ArrayList<Document>();
                documents.add(document);
                collection.insertMany(documents);
            }
        }
        System.out.println("??");
        mongoClient.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:co.aurasphere.mongodb.university.classes.m101j.homework23.MainClass.java

License:Open Source License

/**
 * The main method.//from  w w w .  j a  v a2s  .  com
 *
 * @param args
 *            the arguments
 */
public static void main(String[] args) {

    MongoClient client = new MongoClient();
    MongoDatabase numbersDB = client.getDatabase("students");
    MongoCollection<Document> grades = numbersDB.getCollection("grades");

    // Gets all the grades.
    MongoCursor<Document> cursor = grades.find(Filters.eq("type", "homework"))
            .sort(Sorts.ascending("student_id", "score")).iterator();

    Object previousStudentId = null;
    try {
        // Finds the lowest homework score.
        while (cursor.hasNext()) {
            Document entry = cursor.next();

            // If the student_id is different from the previous one, this 
            // means that this is the student's lowest graded homework 
            // (they are sorted by score for each student).
            if (!entry.get("student_id").equals(previousStudentId)) {
                Object id = entry.get("_id");
                grades.deleteOne(Filters.eq("_id", id));

            }

            // The current document ID becomes the new previous one.   
            previousStudentId = entry.get("student_id");
        }

        // Gets the student with the highest average in the class.
        AggregateIterable<Document> results = grades
                .aggregate(Arrays.asList(Aggregates.group("$student_id", Accumulators.avg("average", "$score")),
                        Aggregates.sort(Sorts.descending("average")), Aggregates.limit(1)));

        // There should be only one result. Prints it.
        System.out.println("Solution : " + results.iterator().next().toJson());

    } finally {
        cursor.close();
    }

    client.close();
}

From source file:co.aurasphere.mongodb.university.classes.m101j.homework31.MainClass.java

License:Open Source License

/**
 * The main method.//from w  ww  .ja  v  a 2  s  .  c  om
 *
 * @param args
 *            the arguments
 */
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    MongoClient client = new MongoClient();
    MongoDatabase db = client.getDatabase("school");
    MongoCollection<Document> students = db.getCollection("students");

    // Gets all the students.
    MongoCursor<Document> cursor = students.find().iterator();

    try {
        while (cursor.hasNext()) {
            Document student = cursor.next();
            List<Document> scores = (List<Document>) student.get("scores");

            // Finds the lowest homework score.
            Document minScoreObj = null;
            double minScore = Double.MAX_VALUE;

            for (Document scoreDocument : scores) {
                double score = scoreDocument.getDouble("score");
                String type = scoreDocument.getString("type");

                // Swaps the scores.
                if (type.equals("homework") && score < minScore) {
                    minScore = score;
                    minScoreObj = scoreDocument;
                }
            }

            // Removes the lowest score.
            if (minScoreObj != null) {
                scores.remove(minScoreObj);
            }

            // Updates the record.
            students.updateOne(Filters.eq("_id", student.get("_id")),
                    new Document("$set", new Document("scores", scores)));
        }

        // Gets the student with the highest average in the class.
        AggregateIterable<Document> results = students.aggregate(Arrays.asList(Aggregates.unwind("$scores"),
                Aggregates.group("$_id", Accumulators.avg("average", "$scores.score")),
                Aggregates.sort(Sorts.descending("average")), Aggregates.limit(1)));

        // There should be only one result. Prints it.
        System.out.println("Solution : " + results.iterator().next().toJson());

    } finally {
        cursor.close();
    }

    client.close();
}

From source file:com.abhishek.mongodb.homework.week2.MongoDBSparkFreemarkerStyle.java

License:Apache License

public static void main(String[] args) {
    final Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(MongoDBSparkFreemarkerStyle.class, "/week2freemarker");

    MongoClient client = new MongoClient();

    MongoDatabase database = client.getDatabase("m101");
    final MongoCollection<Document> collection = database.getCollection("funnynumbers");

    Spark.get(new Route("/") {
        @Override/*from ww  w  . ja  v  a 2  s . co m*/
        public Object handle(final Request request, final Response response) {
            StringWriter writer = new StringWriter();
            try {
                Template template = configuration.getTemplate("answer.ftl");

                // Not necessary yet to understand this.  It's just to prove that you
                // are able to run a command on a mongod server
                List<Document> results = collection.aggregate(asList(
                        new Document("$group",
                                new Document("_id", "$value").append("count", new Document("$sum", 1))),
                        new Document("$match", new Document("count", new Document("$lte", 2))),
                        new Document("$sort", new Document("_id", 1)))).into(new ArrayList<Document>());

                int answer = 0;
                for (Document cur : results) {
                    answer += (Double) cur.get("_id");
                }

                Map<String, String> answerMap = new HashMap<String, String>();
                answerMap.put("answer", Integer.toString(answer));

                template.process(answerMap, writer);
            } catch (Exception e) {
                e.printStackTrace();
                halt(500);
            }
            return writer;
        }
    });
}

From source file:com.acmeair.mongo.services.FlightServiceImpl.java

License:Apache License

@PostConstruct
public void initialization() {
    MongoDatabase database = ConnectionManager.getConnectionManager().getDB();
    flight = database.getCollection("flight");
    flightSegment = database.getCollection("flightSegment");
    airportCodeMapping = database.getCollection("airportCodeMapping");
}

From source file:com.AlertMailerWebPage.servlet.Login.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w w w.j  a  v a 2  s  .  c  o m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        Properties defaultProps = new Properties();
        InputStream in = GetProcessSpeed.class.getResourceAsStream("configuration.properties");
        defaultProps.load(in);
        in.close();
        String database = (String) defaultProps.get("database_mailerDetails");
        String collection = (String) defaultProps.get("collection_mailerDetails");
        String serverAddress = (String) defaultProps.get("IP_AMDBoard");

        /* TODO output your page here. You may use following sample code. */
        final String username = request.getParameter("username");
        final String pass = request.getParameter("password");
        flag = 0;
        // MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017));
        MongoClient mongoClient = new MongoClient();
        MongoDatabase db = mongoClient.getDatabase(database);
        Document query = new Document("login", "login");
        Cookie ck = new Cookie("login", null);
        if (username == null || pass == null) {
            ck.setValue("failed");
            response.addCookie(ck);//adding cookie in the response  
            RequestDispatcher rd = request.getRequestDispatcher("index.html");
            ;
            rd.forward(request, response);
        }
        FindIterable<Document> iterable = db.getCollection(collection).find(query);
        iterable.forEach(new Block<Document>() {
            @Override
            public void apply(final Document document) {
                if (username.equals((String) document.get("username"))
                        && pass.equals((String) document.get("password"))) {
                    flag = 1;
                }
            }
        });
        if (flag == 1) {
            ck.setValue("success");
            response.addCookie(ck);//adding cookie in the response  
            response.sendRedirect("Page1.html");

        } else {

            ck.setValue("failed");
            response.addCookie(ck);//adding cookie in the response  
            RequestDispatcher rd = request.getRequestDispatcher("index.html");
            ;
            rd.forward(request, response);
        }
    }
}

From source file:com.AlertMailerWebPage.servlet.MCRMData.java

public Document getMCRMObject(String database, String collection, String serverAddress, String email) {
    MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress, 27017));
    MongoDatabase db = mongoClient.getDatabase(database);

    FindIterable<Document> iterable = db.getCollection(collection).find(new Document("email", email))
            .projection(new Document("email", 0).append("_id", 0));
    iterable.forEach(new Block<Document>() {
        @Override/*from   www. j  a v a2  s  .  c  om*/
        public void apply(final Document document) {
            MCRMData.document = document;
        }
    });
    return document;
}

From source file:com.AlertMailerWebPage.servlet.MCRMData.java

public void getSL_VM(String database, String collection, String serverAddress, String email) {
    MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress, 27017));
    MongoDatabase db = mongoClient.getDatabase(database);

    FindIterable<Document> iterable = db.getCollection(collection).find(new Document("email", email))
            .sort(new Document("_id", -1)).limit(1);
    iterable.forEach(new Block<Document>() {
        @Override//from  ww  w  . j  ava  2 s .c  om
        public void apply(final Document document) {
            SL = (String) document.get("subject");
            VM = (String) document.get("vmfilename");
        }
    });

}

From source file:com.AlertMailerWebPage.servlet.MCRMData.java

public String getMCRMData(String process, String database, String collection, String serverAddress,
        String email) {//from   w  w w.j av a  2 s  .c om
    process1 = process;
    user = "no data";
    //MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017));
    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase(database);

    FindIterable<Document> iterable = db.getCollection(collection).find(new Document("email", email));
    iterable.forEach(new Block<Document>() {
        @Override
        public void apply(final Document document) {

            if (document.containsKey("" + process1 + "_TOTAL_OPENED")
                    || document.containsKey("" + process1 + "_TOTAL_CONTACTS")) {
                String values = null;
                if (document.containsKey("" + process1 + "_TOTAL_OPENED")
                        && document.containsKey("" + process1 + "_TOTAL_CONTACTS")) {
                    values = "both";
                } else if (document.containsKey("" + process1 + "_TOTAL_OPENED")
                        && !(document.containsKey("" + process1 + "_TOTAL_CONTACTS"))) {
                    values = "mail";
                } else if (!(document.containsKey("" + process1 + "_TOTAL_OPENED"))
                        && document.containsKey("" + process1 + "_TOTAL_CONTACTS")) {
                    values = "contact";
                }
                if (values.equals("both")) {
                    if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 10
                            || (int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 5) {
                        user = "MostActiveUser";

                    } else if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 5
                            || (int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 2) {
                        user = "ActiveUser";
                    } else if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 2
                            || (int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 1) {
                        user = "SlightlyActiveUser";
                    }

                } else if (values.equals("mail")) {
                    if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 10) {
                        user = "MostActiveUser";

                    } else if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 5) {
                        user = "ActiveUser";
                    } else if ((int) document.get("" + process1 + "_TOTAL_OPENED") >= 2) {
                        user = "SlightlyActiveUser";
                    }
                } else if (values.equals("contact")) {
                    if ((int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 5) {
                        user = "MostActiveUser";

                    } else if ((int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 2) {
                        user = "ActiveUser";
                    } else if ((int) document.get("" + process1 + "_TOTAL_CONTACTS") >= 1) {
                        user = "SlightlyActiveUser";
                    }
                }
            }
            if (user.equals("no data")) {

                if (document.containsKey("" + process1 + "1WEEK_OPENED")
                        || document.containsKey("" + process1 + "1WEEK_CONTACTS")) {
                    user = "OtherContact1Week";
                }
            }
            if (user.equals("no data")) {

                if (document.containsKey("" + process1 + "2WEEK_OPENED")
                        || document.containsKey("" + process1 + "2WEEK_CONTACTS")
                        || document.containsKey("" + process1 + "3WEEK_OPENED")
                        || document.containsKey("" + process1 + "3WEEK_CONTACTS")) {
                    user = "OtherContact2n3Week";
                }
            }
            if (user.equals("no data")) {

                if (document.containsKey("WEEK1_TOTAL_TRAFFIC")) {
                    user = "PotentialLead1Week";
                }
            }
            if (user.equals("no data")) {

                if (document.containsKey("WEEK2_TOTAL_TRAFFIC")
                        || document.containsKey("WEEK3_TOTAL_TRAFFIC")) {
                    user = "PotentialLead2n3Week";
                }
            }
            if (user.equals("no data")) {

                if (document.containsKey("" + process1 + "1WEEK_OPENED")
                        || document.containsKey("" + process1 + "1WEEK_CONTACTS")
                        || document.containsKey("" + process1 + "2WEEK_OPENED")
                        || document.containsKey("" + process1 + "2WEEK_CONTACTS")
                        || document.containsKey("" + process1 + "3WEEK_OPENED")
                        || document.containsKey("" + process1 + "3WEEK_CONTACTS")
                        || document.containsKey("" + process1 + "4WEEK_OPENED")
                        || document.containsKey("" + process1 + "4WEEK_CONTACTS")) {
                    user = "RecentInactive";
                }
            }
            if (user.equals("no data")) {
                user = "Inactive";

            }
        }

    });
    return user;
}