List of usage examples for com.mongodb MongoClient getDB
@Deprecated public DB getDB(final String dbName)
From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java
License:Open Source License
public static void Inpdocumentinsert(int id, String FQN, String name, String container, String participant, int inheritanceRelationship_id) { try {/*from w w w . ja v a 2 s. c o m*/ MongoClient mongoClient = new MongoClient(mdlocalhost, mdport); DB db = mongoClient.getDB(tmtextdb); boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray()); DBCollection collection = db.getCollection("inheritanceparticipation"); DBCursor cursor = collection.find(); BasicDBObject document = new BasicDBObject(); document.put("MySQLid", id); document.put("FQN", FQN); document.put("name", name); document.put("container", container); document.put("participant", participant); document.put("inheritanceRelationship_id", inheritanceRelationship_id); collection.insert(document); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java
License:Open Source License
public static void Generaldocumentinsert(String id, String FQN, String name, String container, int potincy, String directtype, String label, String kind) { try {/*from ww w . j a v a2s . c o m*/ MongoClient mongoClient = new MongoClient(mdlocalhost, mdport); DB db = mongoClient.getDB(tmtextdb); boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray()); DBCollection collection = db.getCollection("generalconnection"); DBCursor cursor = collection.find(); BasicDBObject document = new BasicDBObject(); document.put("MySQLid", id); document.put("FQN", FQN); document.put("name", name); document.put("potincy", potincy); document.put("container", container); document.put("directtype", directtype); document.put("label", label); document.put("kind", kind); collection.insert(document); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java
License:Open Source License
public static void Participationdocumentinsert(String id, String name, String FQN, String container, int participant_id, String lower, String upper, String row_name, String whole, String generalConnection) { try {// w w w. j ava 2 s. c o m MongoClient mongoClient = new MongoClient(mdlocalhost, mdport); DB db = mongoClient.getDB(tmtextdb); boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray()); DBCollection collection = db.getCollection("participation"); DBCursor cursor = collection.find(); BasicDBObject document = new BasicDBObject(); document.put("MySQLid", id); document.put("FQN", FQN); document.put("name", name); document.put("container", container); document.put("participant_id", participant_id); document.put("lower", lower); document.put("upper", upper); document.put("row_name", row_name); document.put("whole", whole); document.put("generalConnection", generalConnection); collection.insert(document); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java
License:Open Source License
public static void clearmongodb() { try {/*w w w . java 2 s . c o m*/ MongoClient mongoClient = new MongoClient(mdlocalhost, mdport); DB db = mongoClient.getDB(tmtextdb); boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray()); DBCollection collection1 = db.getCollection("entity"); collection1.drop(); DBCollection collection2 = db.getCollection("attribute"); collection2.drop(); DBCollection collection3 = db.getCollection("deepmodel"); collection3.drop(); DBCollection collection4 = db.getCollection("level"); collection4.drop(); DBCollection collection5 = db.getCollection("binaryconnection"); collection5.drop(); DBCollection collection6 = db.getCollection("inheritanceparticipation"); collection6.drop(); DBCollection collection7 = db.getCollection("inheritancerelationship"); collection7.drop(); DBCollection collection8 = db.getCollection("method"); collection8.drop(); DBCollection collection9 = db.getCollection("package"); collection9.drop(); DBCollection collection10 = db.getCollection("generalconnection"); collection10.drop(); DBCollection collection11 = db.getCollection("participation"); collection11.drop(); // DBCursor cursor = collection.find(); // while(cursor.hasNext()) { // System.out.println(cursor.next()); // } // collection.drop(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
From source file:dinningphilosopher.DinningPhilosopher.java
public static void main(String[] args) { Lock forks[] = new ReentrantLock[5]; try {// w w w . ja v a2 s . co m MongoClient mongoClient = new MongoClient("localhost"); System.out.println("Connection to mongodb successful."); DB db = mongoClient.getDB("mydb"); System.out.println("Database 'mydb' created."); DBCollection coll = db.createCollection("mycol", null); System.out.println("Collection 'mycol' created."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i = 0; i < 5; i++) { forks[i] = new ReentrantLock(); } Thread p1 = new Thread(new Philosopher(forks[4], forks[0], "first")); Thread p2 = new Thread(new Philosopher(forks[0], forks[1], "second")); Thread p3 = new Thread(new Philosopher(forks[1], forks[2], "third")); Thread p4 = new Thread(new Philosopher(forks[2], forks[3], "fourth")); Thread p5 = new Thread(new Philosopher(forks[3], forks[4], "fifth")); p1.start(); p2.start(); p3.start(); p4.start(); p5.start(); }
From source file:dinningphilosopher.DinningPhilosopher.java
private void eat(Lock leftFork, Lock rightFork, String name) throws Exception { leftFork.lock();/*from w w w .ja v a2s . c o m*/ rightFork.lock(); try { MongoClient mongoClient = new MongoClient("localhost"); DB db = mongoClient.getDB("mydb"); DBCollection coll = db.getCollection("mycol"); System.out.println(name + " eating..."); BasicDBObject doc1 = new BasicDBObject(name, " eating..."); coll.insert(doc1); Thread.sleep(1000); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { System.out.println(name + " done eating and now thinking..."); MongoClient mongoClient = new MongoClient("localhost"); DB db = mongoClient.getDB("mydb"); DBCollection coll = db.getCollection("mycol"); BasicDBObject doc2 = new BasicDBObject(name, " done eating and now thinking..."); coll.insert(doc2); leftFork.unlock(); rightFork.unlock(); } }
From source file:dinningphilosopher.DinningPhilosopher.java
public void think(String name) throws Exception { try {/*from w w w . j av a2 s . c o m*/ MongoClient mongoClient = new MongoClient("localhost"); DB db = mongoClient.getDB("mydb"); DBCollection coll = db.getCollection("mycol"); System.out.println(name + " thinking..."); BasicDBObject doc = new BasicDBObject(name, " thinking..."); coll.insert(doc); Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:edu.csulaerp.db.ReferenceMongo.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes no args//w w w . j av a 2 s. co m * @throws UnknownHostException if it cannot connect to a MongoDB instance at localhost:27017 */ public static void main(final String[] args) throws UnknownHostException { // connect to the local database server MongoClient mongoClient = new MongoClient(); /* // Authenticate - optional MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database, password); MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential)); */ // get handle to "mydb" DB db = mongoClient.getDB("mydb"); // get a list of the collections in this database and print them out Set<String> collectionNames = db.getCollectionNames(); for (final String s : collectionNames) { System.out.println(s); } // get a collection object to work with DBCollection coll = db.getCollection("testCollection"); // drop all the data in it coll.drop(); // make a document and insert it BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1) .append("info", new BasicDBObject("x", 203).append("y", 102)); coll.insert(doc); // get it (since it's the only one in there since we dropped the rest earlier on) DBObject myDoc = coll.findOne(); System.out.println(myDoc); // now, lets add lots of little documents to the collection so we can explore queries and cursors for (int i = 0; i < 100; i++) { coll.insert(new BasicDBObject().append("i", i)); } System.out .println("total # of documents after inserting 100 small ones (should be 101) " + coll.getCount()); // lets get all the documents in the collection and print them out DBCursor cursor = coll.find(); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // now use a query to get 1 document out BasicDBObject query = new BasicDBObject("i", 71); cursor = coll.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // $ Operators are represented as strings query = new BasicDBObject("j", new BasicDBObject("$ne", 3)).append("k", new BasicDBObject("$gt", 10)); cursor = coll.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // now use a range query to get a larger subset // find all where i > 50 query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); cursor = coll.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // range query with multiple constraints query = new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30)); cursor = coll.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // Count all documents in a collection but take a maximum second to do so coll.find().maxTime(1, SECONDS).count(); // Bulk operations BulkWriteOperation builder = coll.initializeOrderedBulkOperation(); builder.insert(new BasicDBObject("_id", 1)); builder.insert(new BasicDBObject("_id", 2)); builder.insert(new BasicDBObject("_id", 3)); builder.find(new BasicDBObject("_id", 1)).updateOne(new BasicDBObject("$set", new BasicDBObject("x", 2))); builder.find(new BasicDBObject("_id", 2)).removeOne(); builder.find(new BasicDBObject("_id", 3)).replaceOne(new BasicDBObject("_id", 3).append("x", 4)); BulkWriteResult result = builder.execute(); System.out.println("Ordered bulk write result : " + result); // Unordered bulk operation - no guarantee of order of operation builder = coll.initializeUnorderedBulkOperation(); builder.find(new BasicDBObject("_id", 1)).removeOne(); builder.find(new BasicDBObject("_id", 2)).removeOne(); result = builder.execute(); System.out.println("Ordered bulk write result : " + result); // parallelScan ParallelScanOptions parallelScanOptions = ParallelScanOptions.builder().numCursors(3).batchSize(300) .build(); List<Cursor> cursors = coll.parallelScan(parallelScanOptions); for (Cursor pCursor : cursors) { while (pCursor.hasNext()) { System.out.println(pCursor.next()); } } // release resources db.dropDatabase(); mongoClient.close(); }
From source file:edu.eci.arsw.examples.MongoDBAccessExample.java
public static void main(String ap[]) throws UnknownHostException { MongoClientURI uri = new MongoClientURI("mongodb://test:test@ds031631.mongolab.com:31631/documents"); MongoClient client = new MongoClient(uri); DB db = client.getDB(uri.getDatabase()); DBCollection names = db.getCollection("commontypos"); DBCursor cur = names.find();/*from www . ja va2s. co m*/ while (cur.hasNext()) { DBObject doc = cur.next(); System.out.println(doc.get("words")); System.out.println(doc.get("pocibilidad")); } }
From source file:edu.stanford.epad.common.util.MongoDBOperations.java
License:Open Source License
/** * Connection to mongo database/*from ww w. ja v a2s.c o m*/ * @return */ public static DB getMongoDB() { if (mongoDB == null) { try { String mongoHost = EPADConfig.mongoHostname; if (mongoHost == null) return null; MongoClient mongoClient = null; if (EPADConfig.mongoUser == null) { mongoClient = new MongoClient(mongoHost); } else { MongoCredential credential = MongoCredential.createCredential(EPADConfig.mongoUser, EPADConfig.mongoDB, EPADConfig.mongoPassword.toCharArray()); mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential)); } mongoDB = mongoClient.getDB(EPADConfig.mongoDB); } catch (Exception e) { log.warning("Error connecting to MongoDB", e); } } return mongoDB; }