List of usage examples for com.mongodb MongoClientURI MongoClientURI
public MongoClientURI(final String uri)
From source file:localdomain.localhost.MyServlet.java
License:Apache License
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); writer.println("<html>"); writer.println("<head><title>MyServlet</title></head>"); writer.println("<body><h1>MyServlet</h1>"); writer.println("<h2>MongoDB</h2>"); String uriAsString = System.getProperty("MONGOHQ_URL_MYDB", "mongodb://localhost/local"); MongoClient mongoClient = null;/* w ww . ja v a2 s . co m*/ try { writer.println("<h4>MongoClientURI</h4>"); MongoClientURI uri = new MongoClientURI(uriAsString); writer.println("<p>" + "host=" + uri.getHosts() + ",username=" + uri.getUsername() + ",database=" + uri.getDatabase() + ",collection=" + uri.getCollection() + "</p>"); writer.println("<h4>MongoClient</h4>"); mongoClient = new MongoClient(uri); writer.println("<p>" + mongoClient + "</p>"); writer.println("<h4>Databases</h4>"); try { List<String> databaseNames = mongoClient.getDatabaseNames(); writer.println("<ul>"); for (String databaseName : databaseNames) { writer.println("<li>" + databaseName + (databaseName.equals(uri.getDatabase()) ? " - default database" : "")); } writer.println("</ul>"); } catch (Exception e) { writer.println("<p>Could not list the databases of the MongoDB instance: <code>" + e.getMessage() + "</code></p>"); } writer.println("<h4>Database</h4>"); DB db = mongoClient.getDB(uri.getDatabase()); writer.println("<p>DB: " + db.getName() + "</p>"); writer.println("<h4>Collections</h4>"); Set<String> myCollections = db.getCollectionNames(); if (myCollections.isEmpty()) { writer.println("<p>NO COLLECTIONS!</p>"); } else { writer.println("<ul>"); for (String collection : myCollections) { DBCollection dbCollection = db.getCollection(collection); writer.println( "<li>" + dbCollection.getName() + " - " + dbCollection.getCount() + " entries</li>"); } writer.println("</ul>"); } writer.println("<h4>Success!</h4>"); writer.println("SUCCESS to access the mongodb database"); } catch (Exception e) { writer.println("<code><pre>"); e.printStackTrace(writer); writer.println("</pre></code>"); e.printStackTrace(); } finally { if (mongoClient != null) { try { mongoClient.close(); } catch (Exception e) { e.printStackTrace(); } } } writer.println("</body></html>"); }
From source file:me.carbou.mathieu.tictactoe.di.ServiceBindings.java
License:Apache License
@Provides @Singleton//from ww w. java 2s .c om DB db(Clock clock) throws UnknownHostException { MongoClientURI mongoClientURI = new MongoClientURI(Env.MONGOLAB_URI); MongoClient mongoClient = new MongoClient(mongoClientURI); MongoJsr310.addJsr310EncodingHook(); BSON.addEncodingHook(GString.class, o -> o instanceof GString ? o.toString() : o); BSON.addEncodingHook(ZoneId.class, o -> o instanceof ZoneId ? ((ZoneId) o).getId() : o); BSON.addEncodingHook(Enum.class, o -> o instanceof Enum ? ((Enum) o).name() : o); BSON.addEncodingHook(Locale.class, o -> o instanceof Locale ? o.toString() : o); BSON.addEncodingHook(BigDecimal.class, o -> o instanceof BigDecimal ? ((BigDecimal) o).doubleValue() : o); BSON.addEncodingHook(BigInteger.class, o -> o instanceof BigInteger ? ((BigInteger) o).longValue() : o); return new DB(mongoClient.getDB(mongoClientURI.getDatabase()), clock); }
From source file:me.twister915.punishments.model.storage.mongodb.MongoLoader.java
License:Apache License
@Override public MongoConnection getNewConnection() throws PunishException { ConfigurationSection mongodb = TwistedPunishments.getInstance().getConfig() .getConfigurationSection("mongodb"); String host = mongodb.getString("host", "127.0.0.1"); int port = mongodb.getInt("port", 27017); String username = mongodb.getString("username"); String password = mongodb.getString("psasword"); String database = mongodb.getString("database", "minecraft"); String prefix = mongodb.getString("prefix"); MongoClientURI uri;//w w w . jav a 2 s.c o m if (username == null || password == null) { uri = new MongoClientURI("mongodb://" + host + ":" + port + "/" + database); } else { uri = new MongoClientURI("mongodb://" + username + ":" + password + "@" + host + ":" + "/" + database); } try { return new MongoConnection(prefix, new MongoClient(uri).getDB(database)); } catch (UnknownHostException e) { e.printStackTrace(); throw new PunishException(e.getMessage()); } }
From source file:modules.MongoDBClient.java
private void ConnOpen() { try {//from w w w.ja v a2 s.co m mongoClient = new MongoClient(new MongoClientURI(sURI)); database = mongoClient.getDatabase(sDatabase); collection = database.getCollection(sCollection); uoUpsert.upsert(true); iConnected = 1; } catch (Exception e) { iConnected = 0; } }
From source file:mongodb.QuickTourAdmin.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args/*from w w w . ja v a 2s . co m*/ * takes an optional single argument for the connection string */ public static void main(final String[] args) { MongoClient mongoClient; if (args.length == 0) { // connect to the specified database server mongoClient = new MongoClient("10.9.17.105", 27017); } else { mongoClient = new MongoClient(new MongoClientURI(args[0])); } // get handle to "test" database MongoDatabase database = mongoClient.getDatabase("test"); database.drop(); // get a handle to the "test" collection MongoCollection<Document> collection = database.getCollection("test"); // drop all the data in it collection.drop(); // getting a list of databases for (String name : mongoClient.listDatabaseNames()) { System.out.println(name); } // drop a database mongoClient.getDatabase("databaseToBeDropped").drop(); // create a collection database.createCollection("cappedCollection", new CreateCollectionOptions().capped(true).sizeInBytes(0x100000)); for (String name : database.listCollectionNames()) { System.out.println(name); } // drop a collection: collection.drop(); // create an ascending index on the "i" field // 1 ascending or -1 for descending collection.createIndex(new Document("i", 1)); // list the indexes on the collection for (final Document index : collection.listIndexes()) { System.out.println(index.toJson()); } // create a text index on the "content" field // text indexes to support text search of string content collection.createIndex(new Document("content", "text")); collection.insertOne(new Document("_id", 0).append("content", "textual content")); collection.insertOne(new Document("_id", 1).append("content", "additional content")); collection.insertOne(new Document("_id", 2).append("content", "irrelevant content")); // Find using the text index long matchCount = collection.count(text("textual content -irrelevant")); System.out.println("Text search matches: " + matchCount); // Find using the $language operator Bson textSearch = text("textual content -irrelevant", "english"); matchCount = collection.count(textSearch); System.out.println("Text search matches (english): " + matchCount); // Find the highest scoring match Document projection = new Document("score", new Document("$meta", "textScore")); Document myDoc = collection.find(textSearch).projection(projection).first(); System.out.println("Highest scoring document: " + myDoc.toJson()); // Run a command Document buildInfo = database.runCommand(new Document("buildInfo", 1)); System.out.println(buildInfo); // release resources database.drop(); mongoClient.close(); }
From source file:mongoHandler.Handler.java
public boolean crearConexion() { try {// w ww .ja va2s . c om MongoClientURI uri = new MongoClientURI( "mongodb://prueba1:prueba1@ds039950.mongolab.com:39950/seigcdb"); clienteMongo = new MongoClient(uri); dataBase = clienteMongo.getDB("seigcdb"); System.out.println("CONEXION CREADA"); return true; } catch (UnknownHostException e) { System.out.println("FALLO CONECTANDO:" + e.getMessage()); return false; } catch (Exception s) { System.out.println("FALLO CONECTANDO:" + s.getMessage()); return false; } }
From source file:mongosensors_server.MongoTools.java
License:Open Source License
public MongoTools(String s) { super(new MongoClientURI(s)); }
From source file:mongotwitter.MongoTwitter.java
public MongoTwitter() { String url = "mongodb://" + address; MongoClientURI uri = new MongoClientURI(url); MongoClient mc = new MongoClient(uri); db = mc.getDatabase(database);/*from w w w . ja v a2 s. c o m*/ }
From source file:mx.edu.cide.justiciacotidiana.v1.mongo.MongoInterface.java
License:Open Source License
/** * Crea una instancia de MongoInterface. * @throws UnknownHostException //from w w w.j a v a2 s .com */ private MongoInterface() throws UnknownHostException { if (null != System.getenv("MONGO_URL")) { clientURI = new MongoClientURI(System.getenv("MONGO_URL")); } else { clientURI = new MongoClientURI(MONGOURL); } client = new MongoClient(clientURI); mongoDB = client.getDB(clientURI.getDatabase()); if (null != clientURI.getUsername()) { mongoDB.authenticate(clientURI.getUsername(), clientURI.getPassword()); } }
From source file:mx.iteso.msc.bda.sensorsimulator.dbAccess.java
License:Apache License
public static boolean connectDb() { boolean retval = false; // Error handling it's just a placeholder. It doesn't really really works correctly. try {// w ww . ja v a2 s . co m client = new MongoClient(new MongoClientURI(SERVER_URI)); retval = client != null; db = client.getDatabase(DATABASE); col = db.getCollection(COLLECTION); } catch (Exception ex) { System.out.println(ex.toString()); } return retval; }