Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

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

Prototype

Mongo(final MongoClientURI mongoURI, @Nullable final MongoDriverInformation mongoDriverInformation) 

Source Link

Usage

From source file:IndexService.MongoDB.java

public MongoDB() {

    try {//ww w .  j  ava 2s. com
        mongo = new Mongo("localhost", 27017);
        baseDatos = mongo.getDB("BaseDatos");
        tIndice = baseDatos.getCollection("Indice");
        tPaginas = baseDatos.getCollection("Paginas");
    } catch (UnknownHostException ex) {
        Logger.getLogger(MongoDB.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:interfaces.Rsuarios.java

public Rsuarios() {
    try {/*  w ww  . j a v a 2s  .co m*/

        //realizamos la conexion de la base de datos MOngoDB
        Mongo mongo = new Mongo("localhost", 27017);
        db = mongo.getDB("basedadatos");
        tabla = db.getCollection("usuario");
    } catch (UnknownHostException ex) {
        Logger.getLogger(Rsuarios.class.getName()).log(Level.SEVERE, null, ex);
    } catch (MongoException ex) {
        Logger.getLogger(Rsuarios.class.getName()).log(Level.SEVERE, null, ex);
    }

    //codigo pra centrar, quitar margen y transarencia del formulario
    this.setUndecorated(true);
    initComponents();
    //this.setLocationRelativeTo(null);
    //this.setOpacity(0.9f);

}

From source file:io.github.autsia.crowly.config.PersistenceConfig.java

License:Apache License

@Override
public Mongo mongo() throws Exception {
    return new Mongo(host, port);
}

From source file:Java.MongoDB.java

public MongoDB(String table) {
    m = new Mongo("127.0.0.1", 27017);
    db = m.getDB("GMS");
    dbCol = db.getCollection(table);
}

From source file:jc.mongodb.AbstractMongoIT.java

License:Apache License

@Before
public void createMongo() throws Exception {
    MongoOptions options = new MongoOptions();
    mongo = new Mongo("localhost", options);
    mongo.dropDatabase("testdb");
    db = mongo.getDB("testdb");
    dbCollection = db.getCollection("testcollection");
}

From source file:jp.co.ctc_g.rack.core.config.RackMongoContextConfig.java

License:Apache License

/**
 * MongoDI????/*  w  w  w. j a v a  2 s . c  o  m*/
 * @return Mongo
 * @throws Exception 
 */
@Bean
public Mongo mongo() throws Exception {

    return new Mongo(new ServerAddress(host, port), mongoOptions());
}

From source file:MI_PAQUETE.Ventana.java

public Ventana() {

    this.setTitle("Guadalupe");
    initComponents();//from w  w w.ja v  a  2  s.  com
    this.getContentPane().setBackground(Color.WHITE);
    this.setBounds(10, 10, 345, 51);
    setResizable(false);

    fields[0] = jLabel1;
    fields[1] = jLabel2;
    fields[2] = jLabel3;
    fields[3] = jLabel4;
    fields[4] = jLabel5;
    fields[5] = jLabel6;
    fields[6] = jLabel7;
    fields[7] = jLabel8;
    fields[8] = jLabel9;
    fields[9] = jLabel10;
    fields[10] = jLabel11;
    fields[11] = jLabel12;

    texts[0] = jTextField1;
    texts[1] = jTextField2;
    texts[2] = jTextField3;
    texts[3] = jTextField4;
    texts[4] = jTextField5;
    texts[5] = jTextField6;
    texts[6] = jTextField7;
    texts[7] = jTextField8;
    texts[8] = jTextField9;
    texts[9] = jTextField10;
    texts[10] = jTextField11;
    texts[11] = jTextField12;

    add_to_ActionListenner();
    make_lab_and_text_invisible();

    try {
        Mongo mongo = new Mongo("localhost", 27017);

        db = mongo.getDB("Guadalupe");

    } catch (UnknownHostException ex) {
        Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

/**
 * Initialize any state for this DB. Called once per DB instance; there is
 * one DB instance per client thread./*from  ww w. j  a v a2s  . co m*/
 */
public boolean init() throws DBException {
    // initialize MongoDb driver
    props = getProperties();
    String url = props.getProperty(MONGODB_URL_PROPERTY);
    database = props.getProperty(MONGODB_DB_PROPERTY);
    String writeConcernType = props.getProperty(MONGODB_WRITE_CONCERN_PROPERTY,
            MONGODB_WRITE_CONCERN_PROPERTY_DEFAULT);
    manipulationArray = Boolean.parseBoolean(props.getProperty(MONGODB_MANIPULATION_ARRAY_PROPERTY,
            MONGODB_MANIPULATION_ARRAY_PROPERTY_DEFAULT));
    friendListReq = Boolean.parseBoolean(
            props.getProperty(MONGODB_FRNDLIST_REQ_PROPERTY, MONGODB_FRNDLIST_REQ_PROPERTY_DEFAULT));
    scanResources = Boolean
            .parseBoolean(props.getProperty(MONGODB_SCANFORRES_PROPERTY, MONGODB_SCANFORRES_PROPERTY_DEFAULT));

    if ("none".equals(writeConcernType)) {
        // don't return error on writes
        writeConcern = WriteConcern.NONE;
    } else if ("strict".equals(writeConcernType)) {
        // The write will wait for a response from the server and raise an
        // exception on any error
        writeConcern = WriteConcern.SAFE;
    } else if ("normal".equals(writeConcernType)) {
        // normal error handling - just raise exceptions when problems, don
        // wait for response form servers
        writeConcern = WriteConcern.NORMAL;
    }

    try {
        // System.out.println("new database url = "+url);
        /*
         * MongoOptions mo = new MongoOptions(); mo.connectionsPerHost =
         * 100; mongo = new Mongo(new DBAddress(url), mo);
         */

        /*
         * List<ServerAddress> addrs = new ArrayList<ServerAddress>();
         * addrs.add( new ServerAddress( "10.0.0.122" , 27017 ) );
         * addrs.add( new ServerAddress( "10.0.0.122" , 10002 ) );
         * addrs.add( new ServerAddress( "10.0.0.120" , 10003 ) );
         * MongoOptions mongoOptions = new MongoOptions(); mongo = new
         * Mongo( addrs, mongoOptions);
         * mongo.setReadPreference(ReadPreference.SECONDARY);
         */
        // System.out.println("mongo connection created with "+url);
        try {
            crtcl.acquire();

            if (NumThreads == null) {
                NumThreads = new AtomicInteger();
                NumThreads.set(0);
                MongoOptions mo = new MongoOptions();
                mo.connectionsPerHost = 100;
                String urls[];
                if (!url.contains(";")) { //multiple mongos servers
                    url += "/" + database;
                    mongo = new Mongo(new DBAddress(url), mo);
                } /*else{ // need to append db to url.
                    urls = url.split(";");
                     List<ServerAddress> addrs = new ArrayList<ServerAddress>();
                     for(int i=0; i< urls.length; i++){
                        addrs.add( new ServerAddress(urls[i].split(":")[0] , Integer.parseInt(urls[i].split(":")[1]) ) );
                        //no need to add the database name here as each action does a mongo.getDB(database)
                     } 
                     mongo = new Mongo( addrs);
                  }*/
                else { // need to append db to url.
                    urls = url.split(";");
                    mo = new MongoOptions();
                    mo.connectionsPerHost = 100;
                    //trying to direct clients to different routers
                    url = urls[(Integer.parseInt(props.getProperty(Client.MACHINE_ID_PROPERTY, "0")))
                            % urls.length];
                    url += "/" + database;
                    mongo = new Mongo(new DBAddress(url), mo);
                }

                //mongo = new MongoClient(new DBAddress(url));
                // checking to see if the connection is established
                try {
                    Socket socket = mongo.getMongoOptions().socketFactory.createSocket();
                    socket.connect(mongo.getAddress().getSocketAddress());
                    socket.close();
                } catch (IOException ex) {
                    System.out.println("ERROR: Can't create connection, check if MongDB is running");
                    return false;
                }
            }
            incrementNumThreads();

        } catch (Exception e) {
            System.out.println("MongoDB init failed to acquire semaphore.");
            e.printStackTrace(System.out);
        } finally {
            crtcl.release();
        }
    } catch (Exception e1) {
        System.out.println("Could not initialize MongoDB connection pool for Loader: " + e1.toString());
        e1.printStackTrace(System.out);
        return false;
    }
    return true;
}

From source file:mongoextraction.MongoExtraction.java

public static void main(String[] args) {

    try {/*from   ww w .j av a2 s  .  c o  m*/

        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("twitterdata2");
        DBCollection collection = db.getCollection("tweets2");

        // convert JSON to DBObject directly
        //DBObject dbObject = (DBObject) JSON
        //.parse("{'name':'harika', 'age':24}");

        //collection.insert(dbObject);
        Integer count = 0;
        DBCursor cursorDoc = collection.find();
        while (cursorDoc.hasNext()) {
            count++;
            //System.out.println(cursorDoc.next());
            DBObject dbobj = cursorDoc.next();
            //System.out.println(dbobj);
            String jsonString = dbobj.toString();
            //String loctmp = jsonString.

        }
        System.out.println(count);
        System.out.println("Done");

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:mytubermiserver.mongo.GridFileSystem.java

public void sendVideo(String fileName, InputStream videoStream)
        throws UnknownHostException, MongoException, IOException {
    Mongo mongo = new Mongo(hostAddress, portAddress);

    DB db = mongo.getDB("MyTube");

    // create a "video" namespace
    GridFS gfsPhoto = new GridFS(db, "video");

    //get the file from stream
    GridFSInputFile gfsFile = gfsPhoto.createFile(videoStream);

    // set a new filename for identify purpose
    gfsFile.setFilename(fileName);/* ww w .j  a v a2 s . c o m*/

    // save the image file into mongoDB
    gfsFile.save();
    videoStream.close();
}