@Override public List<JSON> listTables(String db) { MongoDatabase g = getDB(db); List<JSON> list = new ArrayList<JSON>(); if (g != null) { ListCollectionsIterable<Document> it = g.listCollections(); for (Document d : it) { JSON j = JSON.create(); j.put("table_name", d.getString("name")); j.putAll(d); list.add(j); } } Collections.sort(list, new Comparator<JSON>() { @Override public int compare(JSON o1, JSON o2) { return o1.getString("table_name").compareToIgnoreCase(o2.getString("table_name")); } }); return list; }
public static void main(String[] args) throws Exception{ MongoDBDriver mongoDBDriver = new MongoDBDriver(); try{ MongoDBConfig mongoDBConfig = new MongoDBConfig(); //mongoDBConfig.setAddresses("61.171.123.234:27017"); mongoDBConfig.setAddresses("61.171.123.234:27017"); List<MongoDBCredential> credentials = new ArrayList<MongoDBCredential>(); MongoDBCredential credential = new MongoDBCredential(); credential.setDatabaseName("whatsmars-common"); credential.setUsername("whatsmars"); //credential.setPassword("haodai.com"); credential.setPassword("passwordiscommon"); credentials.add(credential); mongoDBConfig.setCredentials(credentials); mongoDBDriver.setConfiguration(mongoDBConfig); mongoDBDriver.init(); MongoDBClient client = new MongoDBClient(); client.setDatabaseName("whatsmars-common"); client.setMongoDBDriver(mongoDBDriver); ListCollectionsIterable<Document> documents = client.getDatabase().listCollections(); MongoCursor<Document> it = documents.iterator(); while (it.hasNext()) { Document item = it.next(); System.out.println(item.toJson()); } it.close(); } catch (Exception e) { e.printStackTrace(); } finally { mongoDBDriver.close(); } }
@Test public void herokuTest() throws UnknownHostException { MongoCredential credential = MongoCredential.createMongoCRCredential("heroku_app25459577", "heroku_app25459577", "gsps2dbe8l7ovbot1jkto5lnid".toCharArray()); MongoClient mongoClient = new MongoClient(new ServerAddress("ds041841.mongolab.com" , 41841), Arrays.asList(credential)); MongoDatabase db = mongoClient.getDatabase("heroku_app25459577"); ListCollectionsIterable<Document> list = db.listCollections(); System.out.println(list.toString()); mongoClient.close(); }
@Override public ListCollectionsIterable<Document> listCollections() { return database.listCollections(); }
@Override public <TResult> ListCollectionsIterable<TResult> listCollections(Class<TResult> resultClass) { return database.listCollections(resultClass); }
@Override public ListCollectionsIterable<Document> listCollections() { return null; }
@Override public <TResult> ListCollectionsIterable<TResult> listCollections(Class<TResult> tResultClass) { return null; }