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:br.edu.ifpb.bdnc.conexao.ConectionMongoDb.java

public static MongoCollection<Document> openConnection() {
    mongoClient = new MongoClient("localhost", 27017);

    MongoDatabase database = mongoClient.getDatabase("databasemongo");

    MongoCollection<Document> collection = database.getCollection("post");
    return collection;
}

From source file:br.inpe.lac.projetoalunoseorientadoresinpe2016.controller.Controller.java

public String getAllPersons() throws Exception {
    MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(100).build();
    MongoClient mongoClient = new MongoClient(new ServerAddress(), options);
    MongoDatabase my_db = mongoClient.getDatabase("alunos_e_orientadores_inpe");
    MongoCollection<Document> pessoas = my_db.getCollection("pessoas");
    List<Document> list = pessoas.find().into(new ArrayList<Document>());
    return new Gson().toJson(list);
}

From source file:ch.hslu.dmg.InitDB.java

public void initProfs() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> prof = database.getCollection("professoren");
    prof.drop();/*from   www  . j  av a 2 s .  co m*/

    ArrayList<Document> profs = new ArrayList<>();
    profs.add(new Document("PersNr", 2125).append("Name", "Sokrates").append("Rang", "C4").append("Raum", 226));
    profs.add(new Document("PersNr", 2126).append("Name", "Russel").append("Rang", "C4").append("Raum", 232));
    profs.add(
            new Document("PersNr", 2127).append("Name", "Kopernikus").append("Rang", "C3").append("Raum", 310));
    profs.add(new Document("PersNr", 2133).append("Name", "Popper").append("Rang", "C3").append("Raum", 52));
    profs.add(
            new Document("PersNr", 2134).append("Name", "Augustinus").append("Rang", "C3").append("Raum", 309));
    profs.add(new Document("PersNr", 2136).append("Name", "Curie").append("Rang", "C4").append("Raum", 36));
    profs.add(new Document("PersNr", 2137).append("Name", "Kant").append("Rang", "C4").append("Raum", 7));
    prof.insertMany(profs);

    mongo.close();
}

From source file:ch.hslu.dmg.InitDB.java

public void initStuds() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> stud = database.getCollection("studenten");
    stud.drop();//from   w w w. ja va2  s  .  c o m

    ArrayList<Document> studs = new ArrayList<>();
    studs.add(new Document("Legi", 24002).append("Name", "Xenokrates").append("Semester", 18));
    studs.add(
            new Document("Legi", 25403).append("Name", "Jonas").append("Semester", 12).append("Hoeren", 5022));
    studs.add(
            new Document("Legi", 26120).append("Name", "Fichte").append("Semester", 10).append("Hoeren", 5001));
    studs.add(new Document("Legi", 26830).append("Name", "Aristoxenos").append("Semester", 8));
    BasicDBList hoeren = new BasicDBList();
    hoeren.add(5001);
    hoeren.add(4052);
    studs.add(new Document("Legi", 27550).append("Name", "Schopenhauer").append("Semester", 6).append("Hoeren",
            hoeren));
    BasicDBList hoeren2 = new BasicDBList();
    hoeren2.add(5041);
    hoeren2.add(5052);
    hoeren2.add(5216);
    hoeren2.add(5259);
    studs.add(new Document("Legi", 28106).append("Name", "Carnap").append("Semester", 3).append("Hoeren",
            hoeren2));
    BasicDBList hoeren3 = new BasicDBList();
    hoeren3.add(5001);
    hoeren3.add(5041);
    hoeren3.add(5049);
    studs.add(new Document("Legi", 29120).append("Name", "Theophrastos").append("Semester", 2).append("Hoeren",
            hoeren3));
    BasicDBList hoeren4 = new BasicDBList();
    hoeren4.add(5022);
    hoeren4.add(5001);
    studs.add(new Document("Legi", 29555).append("Name", "Feuerbach").append("Semester", 2).append("Hoeren",
            hoeren4));
    stud.insertMany(studs);

    mongo.close();
}

From source file:ch.hslu.dmg.InitDB.java

public void initVorlesungen() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> vorlesung = database.getCollection("vorlesungen");
    vorlesung.drop();//from  ww  w  .  ja v a 2s.  c o  m

    ArrayList<Document> vorlesungen = new ArrayList<>();
    vorlesungen.add(new Document("VorlNr", 5001).append("Titel", "Grundzge").append("SWS", 4)
            .append("GelesenVon", 2137));
    vorlesungen.add(
            new Document("VorlNr", 5041).append("Titel", "Ethik").append("SWS", 4).append("GelesenVon", 2125));
    vorlesungen.add(new Document("VorlNr", 5043).append("Titel", "Erkentnistheorie").append("SWS", 3)
            .append("GelesenVon", 2126));
    vorlesungen.add(new Document("VorlNr", 5049).append("Titel", "Maeeutik").append("SWS", 2)
            .append("GelesenVon", 2125));
    vorlesungen.add(
            new Document("VorlNr", 4052).append("Titel", "Logik").append("SWS", 4).append("GelesenVon", 2125));
    vorlesungen.add(new Document("VorlNr", 5052).append("Titel", "Wissenschaftstheorie").append("SWS", 3)
            .append("GelesenVon", 2126));
    vorlesungen.add(new Document("VorlNr", 5216).append("Titel", "Bioethik").append("SWS", 2)
            .append("GelesenVon", 2126));
    vorlesungen.add(new Document("VorlNr", 5259).append("Titel", "Der Wiener Kreis").append("SWS", 2)
            .append("GelesenVon", 2133));
    vorlesungen.add(new Document("VorlNr", 5022).append("Titel", "Glaube und Wissen").append("SWS", 2)
            .append("GelesenVon", 2134));
    vorlesungen.add(new Document("VorlNr", 4630).append("Titel", "Die 3 Kritiken").append("SWS", 4)
            .append("GelesenVon", 2137));

    vorlesung.insertMany(vorlesungen);

    vorlesung.updateOne(eq("VorlNr", 5041), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5001)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5043), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5001)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5049), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5001)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5216), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5041)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5259), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5052)).iterator().next().get("_id"))));
    BasicDBList voraussetzungen = new BasicDBList();
    voraussetzungen.add(vorlesung.find(eq("VorlNr", 5043)).iterator().next().get("_id"));
    voraussetzungen.add(vorlesung.find(eq("VorlNr", 5041)).iterator().next().get("_id"));
    vorlesung.updateOne(eq("VorlNr", 5052),
            new Document("$set", new Document("Voraussetzung", voraussetzungen)));

    mongo.close();
}

From source file:ch.hslu.dmg.InitDB.java

public void initAssistenten() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> assistent = database.getCollection("assistenten");
    assistent.drop();/* w  w  w . j a  v a2  s .  c  o  m*/

    ArrayList<Document> assistenten = new ArrayList<>();
    assistenten.add(new Document("PersNr", 3002).append("Name", "Platon").append("Fachgebiet", "Ideenlehre")
            .append("Boss", 2125));
    assistenten.add(new Document("PersNr", 3003).append("Name", "Aristoteles")
            .append("Fachgebiet", "Syllogistik").append("Boss", 2125));
    assistenten.add(new Document("PersNr", 3004).append("Name", "Wittgenstein")
            .append("Fachgebiet", "Sprachtheorie").append("Boss", 2126));
    assistenten.add(new Document("PersNr", 3005).append("Name", "Rhetikus")
            .append("Fachgebiet", "Planetenbewegung").append("Boss", 2127));
    assistenten.add(new Document("PersNr", 3006).append("Name", "Newton")
            .append("Fachgebiet", "Keplersche Gesetze").append("Boss", 2127));
    assistenten.add(new Document("PersNr", 3007).append("Name", "Spinoza")
            .append("Fachgebiet", "Gott und Natur").append("Boss", 2134));

    assistent.insertMany(assistenten);

    mongo.close();
}

From source file:ch.hslu.dmg.InitDB.java

public void initPruefen() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> pruefungen = database.getCollection("pruefungen");
    pruefungen.drop();// w  w  w . j  a v  a2 s.  c  om

    ArrayList<Document> pruefung = new ArrayList<>();
    pruefung.add(new Document("Legi", 28106).append("VorlNr", 5001).append("PersNr", 2126).append("Note", 1));
    pruefung.add(new Document("Legi", 25403).append("VorlNr", 5041).append("PersNr", 2125).append("Note", 2));
    pruefung.add(new Document("Legi", 27550).append("VorlNr", 4630).append("PersNr", 2137).append("Note", 2));

    pruefungen.insertMany(pruefung);

    mongo.close();
}

From source file:ch.sebooom.observable.mongodb.dao.UserDAO.java

License:Apache License

public UserDAO(final MongoDatabase userDatabase) {
    usersCollection = userDatabase.getCollection("user");
}

From source file:cl.diinf.sdmongo.Principal.java

public static void main(String[] args) {

    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase("test");
    FindIterable<Document> iterable = db.getCollection("restaurants").find();

    iterable.forEach(new Block<Document>() {
        @Override//w  w w  .  j  av a  2 s .c om
        public void apply(final Document document) {
            System.out.println(document);
        }
    });

    System.out.println("Hola mundo");
}

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

License:Open Source License

@Override
public void visit(Page page, CrawlDatums next) {
    System.out.println(page.getUrl());
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    // ?/* www.  j  ava2  s  . c om*/
    MongoDatabase mongoDatabase = mongoClient.getDatabase("douban_crawler");
    System.out.println("Connect to database successfully");
    MongoCollection<org.bson.Document> collection = mongoDatabase.getCollection("moviereview");
    List<Document> documents = new ArrayList<Document>();
    if (page.matchUrl("https://movie.douban.com/review/\\d*/")) {
        index = index + 1;
        //String title = page.select("div[class=article_title]").first().text();
        //String author = page.select("div[id=blog_userface]").first().text();
        //System.out.println("title:" + title + "\tauthor:" + author);
        //HashMap md = page.getMetaData();
        //String review = page.getMetaData("og:description").toString();
        //Document doc = page.getDoc();
        //System.out.println(doc);
        System.out.println("index:" + index);
        String review = page.select("div.main-bd").text();
        System.out.println("review:" + review);
        String movieName = page.select("div.side-back").text();
        System.out.println("movieName:" + movieName);
        ArrayList rc = page.select("div.comment-item");
        Document document = new Document();
        document.put("moviename", movieName);
        document.put("review", review);
        List<String> subreviews = new ArrayList<String>();
        for (int i = 0; i < rc.size(); i++) {
            //System.out.println(rc.get(i).getClass());
            //((org.jsoup.nodes.Element)rc.get(i)).getAllElements().get(3);
            org.jsoup.nodes.Element element = (org.jsoup.nodes.Element) ((org.jsoup.nodes.Element) ((org.jsoup.nodes.Element) rc
                    .get(i)).childNodes().get(3)).childNode(3);
            //Object nodes = ((org.jsoup.nodes.Element)((org.jsoup.nodes.Element)rc.get(i)).childNodes()).getAllElements().get(3);
            String subReview = element.childNode(0).toString();
            System.out.println("subReview:" + subReview);
            if (subReview != "")
                subreviews.add(subReview);
        }
        document.put("subreviews", subreviews);
        collection.insertOne(document);
        //documents.add(document);
        //collection.insertMany(documents);
    } else if (page.matchUrl("https://movie.douban.com/review/best/\\?start=\\d*")) {
        //Document doc = page.getDoc();
        System.out.println("add seed");
        //this.addSeed(page.getUrl());
    }
}