Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

In this page you can find the example usage for com.mongodb MongoClient MongoClient.

Prototype

public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoDriverInformation) 

Source Link

Document

Creates a Mongo described by a URI.

Usage

From source file:calliope.db.MongoConnection.java

License:Open Source License

/**
 * Connect to the database//from  w w  w  .  j a  v a 2s  .  co  m
 * @throws Exception 
 */
private void connect() throws Exception {
    if (db == null) {
        MongoClient mongoClient = new MongoClient(host, MONGO_PORT);
        db = mongoClient.getDB("calliope");
        //boolean auth = db.authenticate( user, password.toCharArray() );
        //if ( !auth )
        //    throw new AeseException( "MongoDB authentication failed");
    }
}

From source file:CapaDato.Conexion.java

public static void main(String[] Args) throws UnknownHostException {
    try {/*from  w w  w. j av a  2s.  c  o m*/
        // Para conectarse al servidor MongoDB
        MongoClient conexion = new MongoClient("localhost", 27017);
        // Ahora conectarse a bases de datos
        DB ejemplo = conexion.getDB("Ejemplo");
        System.out.println("Conectarse a la base de datos exitoso");
        DBCollection coleccion = ejemplo.getCollection("Alumno");
        //             Set<String> collectionNames = ejemplo.getCollectionNames();
        //            for (final String s : collectionNames) 
        //            {
        //            System.out.println(s);
        //            }
        //Para consultar otro mtodo
        Object objeto = new Object();
        objeto = "null,{nombre:1}";
        DBCursor cursor = coleccion.find();

        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }
        } finally {
            cursor.close();
        }

        //DBObject doc = coleccion.findOne();

        //Para consultar
        //DBCursor cursor = coleccion.find ();
        //System.out.println(cursor);
        //            int i = 1;
        //          while (cursor.hasNext ()) 
        //          { 
        //             System.out.println ("insertado documento:" + i); 
        //             System.out.println (cursor.next ()); 
        //             i ++;
        //          }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }

}

From source file:cfel.test.InitAlbum.java

License:Open Source License

/**
 * @param args//www .j  a  v a  2 s.c  o  m
 */
public static void main(String[] args) {
    try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        db = mongoClient.getDB("albumdb");
        fs = new GridFS(db);
        fs.remove((DBObject) null);
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    System.out.println(IMAGE_DIR_PATH);
    addImages(IMAGE_DIR);
}

From source file:ch.bfh.uniboard.persistence.mongodb.ConnectionManagerImpl.java

License:GNU General Public License

@PostConstruct
private void init() {

    Properties props = cm.getConfiguration(CONFIG_NAME);

    if (props == null) {
        logger.log(Level.SEVERE, "Configuration could not be loaded.");
        return;/*from  w w  w.ja  va2 s .  c o m*/
    }
    //DB Connection Information
    String host;
    String dbName;
    String collectionName;
    int port;
    String username;
    String password;
    boolean authentication;
    //Check if values are set or use defaults
    if (props.containsKey(HOST_KEY)) {
        host = props.getProperty(HOST_KEY);
    } else {
        host = "localhost";
    }
    if (props.containsKey(DBNAME_KEY)) {
        dbName = props.getProperty(DBNAME_KEY);
    } else {
        dbName = "uniboard";
    }
    if (props.containsKey(COLLECTION_KEY)) {
        collectionName = props.getProperty(COLLECTION_KEY);
    } else {
        collectionName = "default";
    }
    if (props.containsKey(PORT_KEY)) {
        port = Integer.parseInt(props.getProperty(PORT_KEY));
    } else {
        port = 27017;
    }
    if (props.containsKey(USERNAME_KEY)) {
        username = props.getProperty(USERNAME_KEY);
    } else {
        username = "admin";
    }
    if (props.containsKey(PASSWORD_KEY)) {
        password = props.getProperty(PASSWORD_KEY);
    } else {
        password = "password";
    }
    if (props.containsKey(AUTH_KEY)) {
        authentication = Boolean.parseBoolean(props.getProperty(AUTH_KEY));
    } else {
        authentication = false;
    }

    try {
        if (authentication) {
            MongoCredential credential = MongoCredential.createMongoCRCredential(username, dbName,
                    password.toCharArray());
            //MongoClient already works as a pool if only one instance is used (http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/)
            mongoClient = new MongoClient(new ServerAddress(host, port), Arrays.asList(credential));
        } else {
            mongoClient = new MongoClient(host, port);
        }
        this.connected = true;
    } catch (UnknownHostException ex) {
        logger.log(Level.SEVERE, "DB creation error", ex);
        return;
    }

    //Create or load the database
    DB db = mongoClient.getDB(dbName);

    //create the collection if it does not exist
    if (!db.collectionExists(collectionName)) {
        collection = db.createCollection(collectionName, null);
    }
    //load the collection
    collection = db.getCollection(collectionName);
}

From source file:ch.bfh.uniboard.persistence.mongodb.ConnectionManagerTestImpl.java

License:GNU General Public License

@PostConstruct
private void init() {

    try {//  w w  w  . jav  a  2  s .  co  m
        mongodExe = starter.prepare(new MongodConfigBuilder().version(Version.Main.PRODUCTION)
                .net(new Net(27017, Network.localhostIsIPv6())).build());
        mongod = mongodExe.start();
        mongoClient = new MongoClient(host, port);
        this.connected = true;
    } catch (UnknownHostException ex) {
        logger.log(Level.SEVERE, "DB creation error", ex);
        return;
    } catch (IOException ex) {
        logger.log(Level.SEVERE, "DB creation error", ex);
        return;
    }

    //Create or load the database
    DB db = mongoClient.getDB(dbName);

    //create the collection if it does not exist
    if (!db.collectionExists(collectionName)) {
        collection = db.createCollection(collectionName, null);
    }
    //load the collection
    collection = db.getCollection(collectionName);
}

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();/* w ww.  j a  v  a  2s .  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   ww  w  . jav a 2 s .co 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  w  w w  .  j av a  2 s .  co 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();/*from   w w  w .j  a  v  a 2s.  com*/

    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();/*from   w  w w  .  java 2 s.c o m*/

    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();
}