List of usage examples for com.mongodb.client MongoDatabase getCollection
MongoCollection<Document> getCollection(String collectionName);
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * * @param doc/*ww w .j a v a2 s.co m*/ * @return el numero de documentos en la coleccion */ public Integer count(Bson filter) { try { contador = 0; MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(filter); iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { contador++; } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "count()").log(Level.SEVERE, null, e); exception = new Exception("count()", e); } } }); } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "count()").log(Level.SEVERE, null, e); exception = new Exception("count()", e); } return contador; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * * @param document// ww w . j av a 2s. c o m * @return */ public List<T> findAll(Document... docSort) { list = new ArrayList<>(); Document sortQuery = new Document(); try { if (docSort.length != 0) { sortQuery = docSort[0]; } MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find().sort(sortQuery); list = iterableList(iterable); } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findAll() ", e); new JmoordbException("findAll()"); } return list; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * Busca con paginacion en una coleccion * * @param document/*from w w w . j av a2 s. c o m*/ * @return */ public List<T> findPagination(Integer pageNumber, Integer rowsForPage, Document... docSort) { list = new ArrayList<>(); Document sortQuery = new Document(); try { if (docSort.length != 0) { sortQuery = docSort[0]; } MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find() .skip(pageNumber > 0 ? ((pageNumber - 1) * rowsForPage) : 0).limit(rowsForPage).sort(sortQuery); list = iterableList(iterable); } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findPagination() ", e); new JmoordbException("findPagination()"); } return list; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * Busca con paginacion en una coleccion con filtro * * @param document/* w w w. j av a2 s.co m*/ * @return */ public List<T> findPagination(Document filter, Integer pageNumber, Integer rowsForPage, Document... docSort) { list = new ArrayList<>(); Document sortQuery = new Document(); try { if (docSort.length != 0) { sortQuery = docSort[0]; } MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(filter) .skip(pageNumber > 0 ? ((pageNumber - 1) * rowsForPage) : 0).limit(rowsForPage).sort(sortQuery); list = iterableList(iterable); } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findPagination() ", e); new JmoordbException("findPagination()"); } return list; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * Busca con paginacion en una coleccion con filtro * * @param document//w ww . j av a 2 s . com * @return */ public List<T> findPagination(String sql, Integer pageNumber, Integer rowsForPage) { list = new ArrayList<>(); Document sortQuery = new Document(); Document doc = new Document(); try { QueryConverter queryConverter = new QueryConverter(sql); MongoDBQueryHolder mongoDBQueryHolder = queryConverter.getMongoQuery(); String collection = mongoDBQueryHolder.getCollection(); doc = mongoDBQueryHolder.getQuery(); Document projection = mongoDBQueryHolder.getProjection(); if (sql.toLowerCase().indexOf("order by") != -1) { sortQuery = mongoDBQueryHolder.getSort(); } MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(doc) .skip(pageNumber > 0 ? ((pageNumber - 1) * rowsForPage) : 0).limit(rowsForPage).sort(sortQuery); list = iterableList(iterable); } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findPagination() ", e); new JmoordbException("findPagination()"); } return list; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * * @param key/*from w w w .ja v a 2 s . c o m*/ * @param value * @param field * @return */ public T findOneAndUpdate(String key, String value, String field, Integer... incremento) { try { Integer increment = 1; if (incremento.length != 0) { increment = incremento[0]; } Document doc = new Document(key, value); Document inc = new Document("$inc", new Document(field, increment)); FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions(); findOneAndUpdateOptions.upsert(true); findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER); Object t = entityClass.newInstance(); MongoDatabase db = getMongoClient().getDatabase(database); Document iterable = db.getCollection(collection).findOneAndUpdate(doc, inc, findOneAndUpdateOptions); try { t1 = (T) documentToJava.fromDocument(entityClass, iterable, embeddedBeansList, referencedBeansList); } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } return t1; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * findOneAndUpdate/*from w w w . jav a 2 s .c om*/ * * @param doc * @param field * @param incremento * @return */ public T findOneAndUpdate(Document doc, String field, Integer... incremento) { try { Integer increment = 1; if (incremento.length != 0) { increment = incremento[0]; } Document inc = new Document("$inc", new Document(field, increment)); FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions(); findOneAndUpdateOptions.upsert(true); findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER); Object t = entityClass.newInstance(); list = new ArrayList<>(); MongoDatabase db = getMongoClient().getDatabase(database); Document iterable = db.getCollection(collection).findOneAndUpdate(doc, inc, findOneAndUpdateOptions); try { t1 = (T) documentToJava.fromDocument(entityClass, iterable, embeddedBeansList, referencedBeansList); // Method method = entityClass.getDeclaredMethod("toPojo", Document.class); // list.add((T) method.invoke(t, iterable)); } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } return t1; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * * @param doc/*from w ww. j ava 2 s.c o m*/ * @param inc * @param incremento * @return */ public T findOneAndUpdate(Document doc, Document inc, Integer... incremento) { try { FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions(); findOneAndUpdateOptions.upsert(true); findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER); Object t = entityClass.newInstance(); list = new ArrayList<>(); MongoDatabase db = getMongoClient().getDatabase(database); Document iterable = db.getCollection(collection).findOneAndUpdate(doc, inc, findOneAndUpdateOptions); try { t1 = (T) documentToJava.fromDocument(entityClass, iterable, embeddedBeansList, referencedBeansList); } catch (Exception e) { Logger.getLogger(Repository.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } return t1; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * * @param doc//w w w. j av a2s. co m * @param docSort * @return */ public List<T> findBy(Document doc, Document... docSort) { Document sortQuery = new Document(); try { if (docSort.length != 0) { sortQuery = docSort[0]; } list = new ArrayList<>(); MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(doc).sort(sortQuery); list = iterableList(iterable); } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findBy() ", e); } return list; }
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * * @param doc//from w w w . jav a 2 s . c om * @param docSort * @return */ public List<T> findBy(Bson builder, Document... docSort) { Document sortQuery = new Document(); try { if (docSort.length != 0) { sortQuery = docSort[0]; } list = new ArrayList<>(); MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(builder).sort(sortQuery); list = iterableList(iterable); } catch (Exception e) { Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findBy() ", e); } return list; }