List of usage examples for com.mongodb MongoClient close
public void close()
From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java
License:Apache License
public DBObject findMongoDocument(ServerConfiguration configuration, MongoCollection mongoCollection, Object _id) {// w w w .j a v a2s . co m MongoClient mongo = null; try { String databaseName = mongoCollection.getDatabaseName(); mongo = createMongoClient(configuration); DB database = mongo.getDB(databaseName); DBCollection collection = database.getCollection(mongoCollection.getName()); return collection.findOne(new BasicDBObject("_id", _id)); } catch (UnknownHostException ex) { throw new ConfigurationException(ex); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java
License:Apache License
public void connect(ServerConfiguration configuration) { MongoClient mongo = null; try {//from w ww . ja v a 2 s . c o m String userDatabase = configuration.getUserDatabase(); mongo = createMongoClient(configuration); MongoIterable<String> collectionNames; if (StringUtils.isNotEmpty(userDatabase)) { collectionNames = mongo.getDatabase(userDatabase).listCollectionNames(); } else { collectionNames = mongo.getDatabase("test").listCollectionNames(); } collectionNames.first(); } catch (IOException ex) { throw new MongoConnectionException(ex); } catch (MongoException ex) { LOG.error("Error when accessing Mongo server", ex); throw new MongoConnectionException(ex.getMessage()); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java
License:Apache License
List<Database> loadDatabaseCollections(ServerConfiguration configuration) { MongoClient mongo = null; List<Database> mongoDatabases = new LinkedList<>(); try {// w w w .j av a2 s .c o m String userDatabase = configuration.getUserDatabase(); mongo = createMongoClient(configuration); if (StringUtils.isNotEmpty(userDatabase)) { MongoDatabase database = mongo.getDatabase(userDatabase); mongoDatabases.add(createMongoDatabaseAndItsCollections(database)); } else { MongoIterable<String> databaseNames = mongo.listDatabaseNames(); for (String databaseName : databaseNames) { MongoDatabase database = mongo.getDatabase(databaseName); mongoDatabases.add(createMongoDatabaseAndItsCollections(database)); } Collections.sort(mongoDatabases, new Comparator<Database>() { @Override public int compare(Database o1, Database o2) { return o1.getName().compareTo(o2.getName()); } }); } return mongoDatabases; } catch (MongoException | UnknownHostException mongoEx) { throw new ConfigurationException(mongoEx); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java
License:Apache License
public void update(ServerConfiguration configuration, SingleMongoCollection singleMongoCollection, Document mongoDocument) { MongoClient mongo = null; try {//from w ww . j a v a 2s . c o m String databaseName = singleMongoCollection.getDatabaseName(); mongo = createMongoClient(configuration); MongoDatabase database = mongo.getDatabase(databaseName); MongoCollection<Document> collection = database.getCollection(singleMongoCollection.getName()); final Object id = mongoDocument.get("_id"); if (id == null) { collection.insertOne(mongoDocument); } else { collection.replaceOne(Filters.eq("_id", id), mongoDocument); } } catch (UnknownHostException ex) { throw new ConfigurationException(ex); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java
License:Apache License
public void delete(ServerConfiguration configuration, SingleMongoCollection singleMongoCollection, Object _id) { MongoClient mongo = null; try {//from ww w.j a v a 2 s . c o m String databaseName = singleMongoCollection.getDatabaseName(); mongo = createMongoClient(configuration); MongoDatabase database = mongo.getDatabase(databaseName); MongoCollection<Document> collection = database.getCollection(singleMongoCollection.getName()); collection.deleteOne(Filters.eq("_id", _id)); } catch (UnknownHostException ex) { throw new ConfigurationException(ex); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java
License:Apache License
public void dropCollection(ServerConfiguration configuration, SingleMongoCollection singleMongoCollection) { MongoClient mongo = null; try {/*from ww w . j a v a 2 s.c om*/ String databaseName = singleMongoCollection.getDatabaseName(); mongo = createMongoClient(configuration); mongo.getDatabase(databaseName).getCollection(singleMongoCollection.getName()).drop(); } catch (UnknownHostException ex) { throw new ConfigurationException(ex); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java
License:Apache License
public void dropDatabase(ServerConfiguration configuration, SingleMongoDatabase selectedDatabase) { MongoClient mongo = null; try {//from w ww . j av a 2s . c o m mongo = createMongoClient(configuration); mongo.dropDatabase(selectedDatabase.getName()); } catch (UnknownHostException ex) { throw new ConfigurationException(ex); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java
License:Apache License
public MongoResult loadCollectionValues(ServerConfiguration configuration, SingleMongoCollection singleMongoCollection, MongoQueryOptions mongoQueryOptions) { MongoClient mongo = null; try {//from w w w . j a v a 2 s .c om String databaseName = singleMongoCollection.getDatabaseName(); mongo = createMongoClient(configuration); MongoDatabase database = mongo.getDatabase(databaseName); MongoCollection<Document> collection = database.getCollection(singleMongoCollection.getName()); MongoResult mongoResult = new MongoResult(singleMongoCollection.getName()); if (mongoQueryOptions.isAggregate()) { return aggregate(mongoQueryOptions, mongoResult, collection); } return find(mongoQueryOptions, mongoResult, collection); } catch (UnknownHostException ex) { throw new ConfigurationException(ex); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java
License:Apache License
public Document findMongoDocument(ServerConfiguration configuration, SingleMongoCollection singleMongoCollection, Object _id) { MongoClient mongo = null; try {//from www . java2 s. co m String databaseName = singleMongoCollection.getDatabaseName(); mongo = createMongoClient(configuration); MongoDatabase database = mongo.getDatabase(databaseName); com.mongodb.client.MongoCollection<Document> collection = database .getCollection(singleMongoCollection.getName()); return collection.find(Filters.eq("_id", _id)).first(); } catch (UnknownHostException ex) { throw new ConfigurationException(ex); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.craftercms.commons.mongo.MongoClientFromUriFactoryBean.java
License:Open Source License
@Override protected void destroyInstance(MongoClient mongoClient) throws Exception { mongoClient.close(); }