List of usage examples for com.mongodb MongoClient getDB
@Deprecated public DB getDB(final String dbName)
From source file:localdomain.localhost.MongoDbTroubleshooter.java
License:Open Source License
public void run() throws Exception { try (PrintWriter writer = new PrintWriter(System.out)) { MongoClientURI mongoClientUri = new MongoClientURI(uri); writer.println("" + "host=" + mongoClientUri.getHosts() + ",username=" + mongoClientUri.getUsername() + ",database=" + mongoClientUri.getDatabase() + ",collection=" + mongoClientUri.getCollection() + ""); writer.println();//from w w w . jav a2 s . c o m writer.println(); writer.println("# MongoClient"); writer.println(); MongoClient mongoClient = new MongoClient(mongoClientUri); writer.println("" + mongoClient + ""); writer.println(); writer.println(); writer.println("# Databases"); writer.println(); try { List<String> databaseNames = mongoClient.getDatabaseNames(); for (String databaseName : databaseNames) { writer.println("* " + databaseName + (databaseName.equals(mongoClientUri.getDatabase()) ? " - default database" : "")); } } catch (Exception e) { writer.println("Could not list the databases of the MongoDB instance: '" + e.getMessage() + "'"); } writer.println(); writer.println(); writer.println("# Database"); writer.println(); DB db = mongoClient.getDB(mongoClientUri.getDatabase()); writer.println("DB: " + db.getName() + ""); writer.println(); writer.println(); writer.println("## Collections"); writer.println(); Set<String> myCollections = db.getCollectionNames(); if (myCollections.isEmpty()) { writer.println("NO COLLECTIONS!"); } else { for (String collection : myCollections) { DBCollection dbCollection = db.getCollection(collection); writer.println("* " + dbCollection.getName() + " - " + dbCollection.getCount() + " entries"); } } } }
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; try {//from ww w . ja va2 s .c o m 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:lucenetools.DocIndexerMongo.java
License:Apache License
/** * This method creates the Mongo cursor object for the connection * details specified in the config file. * /*from w w w . j a v a 2 s. c o m*/ * @param opts options object * @return DBCursor mongo cursor * @throws UnknownHostException cannot connect to mongo host */ static DBCursor getCursor(Options opts) throws UnknownHostException { MongoClient mongoClient = new MongoClient(opts.host, opts.port); DB db = mongoClient.getDB(opts.database); DBCollection coll = db.getCollection(opts.collection); DBCursor cursor = coll.find(); return cursor; }
From source file:me.carbou.mathieu.tictactoe.di.ServiceBindings.java
License:Apache License
@Provides @Singleton/*from w w w . j a v a 2 s . c o m*/ 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.yyam.mongodbutils.MongoDbOperater.java
public static void main(String[] args) throws Exception { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("test"); DBCollection coll = db.getCollection("rztest"); String sql = "select * from table1 where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3') order by age,name limit 123"; MongoDbOperater ope = new MongoDbOperater(); ope.setMongoClient(mongoClient);//w ww . j av a 2s .c om QueryInfo info = ope.sql2QueryInfo("abc", sql); System.out.println(info.debugStr()); sql = "select * from algoflow_instance_log where functionName='f1' and reuseResult=false and returnCode=0 and action='LEAVE' order by timestamp desc limit 100"; info = ope.sql2QueryInfo("abc", sql); System.out.println(info.debugStr()); String updateSql = "update table1 set a=1, b='sdaf', c='2012-11-23 17:45:32' where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3')"; QueryInfo uinfo = ope.sql2QueryInfo("abc", updateSql); System.out.println(uinfo.debugStr()); String json = "{\"a\":123,\"b\":345}"; BasicBSONObject obj = (BasicBSONObject) JSON.parse(json, new DefaultDBCallback(coll)); BasicDBObject dbObj = new BasicDBObject(obj); System.out.println(dbObj); }
From source file:mini_mirc_server.miniIRCHandler.java
/** * Add a user to a channel (can check if the channel is a new channel) * @param Username/*www .j a v a 2 s. c om*/ * @param Channel * @return code */ private int AddChannel(String Username, String Channel) { int ret = 0; try { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll = db.getCollection("channelCollection"); BasicDBObject query = new BasicDBObject("username", Username).append("channel", Channel); DBCursor cursor = coll.find(query); try { if (cursor.hasNext()) { ret = 1; System.err.println(Username + " has joined Channel : " + Channel + "!"); } else { query = new BasicDBObject("channel", Channel); DBCursor cursor2 = coll.find(query); try { if (!cursor2.hasNext()) { ret = 2; } } finally { cursor2.close(); } BasicDBObject doc = new BasicDBObject("username", Username).append("channel", Channel); coll.insert(doc); System.out.println(Username + " joined Channel : " + Channel); } } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex); } return ret; }
From source file:mini_mirc_server.miniIRCHandler.java
/** * Delete a user from a channel (used in leave method) * @param Channel// ww w . ja v a2 s . c om * @param Username * @return code */ private int DeleteChannelUser(String Channel, String Username) { int ret = 0; try { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll = db.getCollection("channelCollection"); BasicDBObject query = new BasicDBObject("username", Username).append("channel", Channel); DBCursor cursor = coll.find(query); try { while (cursor.hasNext()) { coll.remove(cursor.next()); } } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(Username + " has leaved Channel : " + Channel); return ret; }
From source file:mini_mirc_server.miniIRCHandler.java
/** * Delete a user in all channel (used in cleaning) * @param Username/*from w ww . j a v a 2s .c o m*/ * @return code */ public int DeleteUserInChannel(String Username) { int ret = 0; try { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll = db.getCollection("channelCollection"); BasicDBObject query = new BasicDBObject("username", Username); DBCursor cursor = coll.find(query); try { while (cursor.hasNext()) { coll.remove(cursor.next()); } } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(Username + " has been deleted in Channel Collection!"); return ret; }
From source file:mini_mirc_server.miniIRCHandler.java
/** * Register a user to the server (used in RegUser) * @param username/* ww w .jav a2s. c o m*/ * @return code */ private int AddUser(String username) { int ret = 0; try { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll = db.getCollection("activeUser"); BasicDBObject query = new BasicDBObject("username", username); DBCursor cursor = coll.find(query); try { if (cursor.hasNext()) { Date now = new Date(); long timestamp_now = now.getTime(); long treshold = timestamp_now - (1000 * 10); //10 BasicDBObject temp = (BasicDBObject) cursor.next(); Date time_temp = (Date) temp.get("timestamp"); long timestamp_temp = time_temp.getTime(); if (timestamp_temp < treshold) { ret = 2; System.out.println(username + " has joined back!"); } else { ret = 1; System.err.println(username + " has been used !"); } } else { java.util.Date date = new java.util.Date(); BasicDBObject doc = new BasicDBObject("username", username).append("timestamp", date); coll.insert(doc); System.out.println(username + " online !"); } } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex); } return ret; }
From source file:mini_mirc_server.miniIRCHandler.java
private int UpdateLastActive(String username) { int ret = 0;/*from w w w. j a v a 2s.c o m*/ try { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll = db.getCollection("activeUser"); BasicDBObject query = new BasicDBObject("username", username); DBCursor cursor = coll.find(query); try { if (cursor.hasNext()) { DBObject temp = cursor.next(); java.util.Date date = new java.util.Date(); temp.put("timestamp", date); coll.save(temp); } else { java.util.Date date = new java.util.Date(); BasicDBObject doc = new BasicDBObject("username", username).append("timestamp", date); coll.insert(doc); System.out.println(username + " online !"); } } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex); } return 0; }