List of usage examples for com.mongodb MongoClient getDatabaseNames
@Deprecated
public List<String> getDatabaseNames()
From source file:org.pentaho.mongo.MongoUtils.java
License:Open Source License
/** * Retrieve all database names found in MongoDB as visible by the * authenticated user.//from w ww . j a va 2 s .c o m * * @param meta * Input meta with connection information * @param varSpace * Variable space to substitute variables with * @return A list of database names found in MongoDB * @throws KettleException */ public static List<String> getDatabaseNames(final MongoDbInputMeta meta, final VariableSpace varSpace) throws KettleException { try { AuthContext context = MongoUtils.createAuthContext(meta, varSpace); return context.doAs(new PrivilegedExceptionAction<List<String>>() { @Override public List<String> run() throws Exception { MongoClient conn = null; try { conn = MongoDbInputData.initConnection(meta, varSpace, null); return conn.getDatabaseNames(); } finally { if (conn != null) { conn.close(); } } } }); } catch (PrivilegedActionException ex) { if (ex.getCause() instanceof KettleException) { throw (KettleException) ex.getCause(); } else { throw new KettleException("Unable to retrieve database names from MongoDB", ex.getCause()); } } }
From source file:pruebamongo.PruebaMongo.java
private static void imprimirBasesDatos(MongoClient mongo) { List basesdatos = mongo.getDatabaseNames(); for (int i = 0; i < basesdatos.size(); i++) { System.out.println("-" + basesdatos.get(i).toString()); }/*from w w w . j a v a 2s. c o m*/ }
From source file:pruebamongo.PruebaMongo.java
private static void obtenerColecciones(MongoClient mongo) { List basesDatos = mongo.getDatabaseNames(); for (int i = 0; i < basesDatos.size(); i++) { //Obtenemos las bases de datos existentes: String nombredb = mongo.getDB(basesDatos.get(i).toString()).toString(); System.out.println("-" + nombredb); //Usamos una base de datos en concreto: //if(!nombredb.equals("local")){ DB db = mongo.getDB(nombredb);//ww w . j ava 2 s .c o m //Obtenemos el nombre de las colecciones que contiene la base de datos: Set colecciones = db.getCollectionNames(); //Imprimimos la lista de colecciones si hay colecciones que mostrar: if (!colecciones.isEmpty()) for (int j = 0; j < colecciones.size(); j++) { Object col[] = colecciones.toArray(); System.out.println(" * " + col[j]); } else System.out.println(" * Sin colecciones."); } }
From source file:spntoolsdata.conn.connection.java
private void PrintDataBase(MongoClient mongo) { List dbs = mongo.getDatabaseNames(); for (int i = 0; i < dbs.size(); i++) { System.out.println(" - " + dbs.get(i).toString()); }/* ww w .java 2 s . c o m*/ }
From source file:tango.mongo.MongoConnector.java
License:Open Source License
public static boolean isMongoOn(String host) { MongoClient m; m = null;/*from w w w . j a v a 2 s.c o m*/ //try { m = new MongoClient(host); // } catch (UnknownHostException e) { // exceptionPrinter.print(e, "ukhe:", Core.GUIMode); // return false; //} if (m == null) { return false; } List<String> l; try { MongoIterable ll = m.listDatabaseNames(); l = m.getDatabaseNames(); } catch (MongoException e) { if (Core.GUIMode) { IJ.log("connection failed.."); } return false; } return true; }
From source file:teste.mongo.example.QuickTourAdmin.java
License:Apache License
public static void main(String[] args) throws Exception { // connect to the local database server MongoClient mongoClient = new MongoClient(); /*/*from w ww .j a v a 2 s .c om*/ // Authenticate - optional MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database, password); MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential)); */ System.out.println("##### DataBaseInitial"); // get db names for (String s : mongoClient.getDatabaseNames()) { System.out.println(s); } // get a db DB db = mongoClient.getDB("mydb"); System.out.println("##### With new DataBase MYDB"); // do an insert so that the db will really be created. Calling getDB() doesn't really take any // action with the server db.getCollection("testcollection").insert(new BasicDBObject("i", 1)); for (String s : mongoClient.getDatabaseNames()) { System.out.println(s); } // drop a database mongoClient.dropDatabase("mydb"); System.out.println("##### DataBase drop MYDB"); for (String s : mongoClient.getDatabaseNames()) { System.out.println(s); } // create a collection db = mongoClient.getDB("mydb"); db.createCollection("testCollection", new BasicDBObject("capped", true).append("size", 1048576)); System.out.println("##### ListAllCollection MYDB"); // List all collections for (String s : db.getCollectionNames()) { System.out.println(s); } // Dropping a collection DBCollection testCollection = db.getCollection("testCollection"); testCollection.drop(); System.out.println("##### ListAllCollection MYDB - Drop testCollection"); System.out.println(db.getCollectionNames()); /* Indexes */ // get a collection object to work with DBCollection coll = db.getCollection("testCollection"); // drop all the data in it coll.drop(); // create an index on the "i" field coll.createIndex(new BasicDBObject("i", 1)); // Geospatial query coll.createIndex(new BasicDBObject("loc", "2dsphere")); BasicDBList coordinates = new BasicDBList(); coordinates.put(0, -73.97); coordinates.put(1, 40.77); coll.insert(new BasicDBObject("name", "Central Park") .append("loc", new BasicDBObject("type", "Point").append("coordinates", coordinates)) .append("category", "Parks")); coordinates.put(0, -73.88); coordinates.put(1, 40.78); coll.insert(new BasicDBObject("name", "La Guardia Airport") .append("loc", new BasicDBObject("type", "Point").append("coordinates", coordinates)) .append("category", "Airport")); // Find whats within 500m of my location BasicDBList myLocation = new BasicDBList(); myLocation.put(0, -73.965); myLocation.put(1, 40.769); DBObject myDoc = coll.findOne(new BasicDBObject("loc", new BasicDBObject("$near", new BasicDBObject("$geometry", new BasicDBObject("type", "Point").append("coordinates", myLocation)) .append("$maxDistance", 500)))); System.out.println(myDoc.get("name")); // create a text index on the "content" field coll.createIndex(new BasicDBObject("content", "text")); coll.insert(new BasicDBObject("_id", 0).append("content", "textual content")); coll.insert(new BasicDBObject("_id", 1).append("content", "additional content")); coll.insert(new BasicDBObject("_id", 2).append("content", "irrelevant content")); // Find using the text index BasicDBObject search = new BasicDBObject("$search", "textual content -irrelevant"); BasicDBObject textSearch = new BasicDBObject("$text", search); int matchCount = coll.find(textSearch).count(); System.out.println("Text search matches: " + matchCount); // Find using the $language operator textSearch = new BasicDBObject("$text", search.append("$language", "english")); matchCount = coll.find(textSearch).count(); System.out.println("Text search matches (english): " + matchCount); // Find the highest scoring match BasicDBObject projection = new BasicDBObject("score", new BasicDBObject("$meta", "textScore")); myDoc = coll.findOne(textSearch, projection); System.out.println("Highest scoring document: " + myDoc); // list the indexes on the collection List<DBObject> list = coll.getIndexInfo(); for (final DBObject o : list) { System.out.println(o); } // clean up mongoClient.dropDatabase("mydb"); mongoClient.close(); }