List of usage examples for com.mongodb.client MongoDatabase getCollection
MongoCollection<Document> getCollection(String collectionName);
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
public T search(String key, Object value) { try {/*from w w w . j ava2 s .c o m*/ // Object t = entityClass.newInstance(); MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value)); haveElements = false; iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { haveElements = true; tlocal = (T) documentToJava.fromDocument(entityClass, document, embeddedBeansList, referencedBeansList); } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName() + "find()").log(Level.SEVERE, null, e); exception = new Exception("find() ", e); } } }); if (haveElements) { return tlocal; } return null; } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("find() ", e); } return null; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
/** * * @param document/* w w w . j ava 2 s. co m*/ * @return */ @Override public Optional<T> find(Document document) { try { // Object t = entityClass.newInstance(); MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(document); tlocal = (T) iterableSimple(iterable); return Optional.of(tlocal); //return (T) tlocal; } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("find() ", e); new JmoordbException("find()"); } return Optional.empty(); // return null; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
private T findInternal(Document document) { try {/*www . j av a 2 s . c o m*/ // Object t = entityClass.newInstance(); MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(document); tlocal = (T) iterableSimple(iterable); return tlocal; //return (T) tlocal; } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("find() ", e); new JmoordbException("find()"); } return null; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
/** * * @param doc/*from ww w .ja v a 2s .c om*/ * @return el numero de documentos en la coleccion */ public Integer count(Document... doc) { try { contador = 0; Document documento = new Document(); if (doc.length != 0) { documento = doc[0]; MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(documento); iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { try { contador++; } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName() + "count()").log(Level.SEVERE, null, e); exception = new Exception("count()", e); } } }); } else { // no tiene parametros contador = (int) getMongoClient().getDatabase(database).getCollection(collection).count(); } } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName() + "count()").log(Level.SEVERE, null, e); exception = new Exception("count()", e); } return contador; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
/** * * @param document//ww w . ja v a 2 s .c om * @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(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("find() ", e); new JmoordbException("find()"); } return list; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
/** * * @param key/*from w w w.j a va2s.c om*/ * @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(AbstractFacade.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } return t1; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
/** * findOneAndUpdate/*from w w w. j av a 2 s .c o m*/ * * @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(AbstractFacade.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } return t1; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
/** * * @param doc/*from www . j av a2s.com*/ * @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); // Method method = entityClass.getDeclaredMethod("toPojo", Document.class); // list.add((T) method.invoke(t, iterable)); } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findOneAndUpdate()", e); } return t1; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
/** * * @param doc//w ww . j a va 2s .com * @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(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findBy() ", e); } return list; }
From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java
public List<T> findBy(String key, Object value, Document... docSort) { Document sortQuery = new Document(); try {/* www . j av a2 s. c o m*/ if (docSort.length != 0) { sortQuery = docSort[0]; } list = new ArrayList<>(); Document doc = new Document(key, value); MongoDatabase db = getMongoClient().getDatabase(database); FindIterable<Document> iterable = db.getCollection(collection).find(doc).sort(sortQuery); list = iterableList(iterable); } catch (Exception e) { Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e); exception = new Exception("findBy() ", e); } return list; }