Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:com.analogmountains.flume.MongoSink.java

License:Open Source License

@Override
public synchronized void start() {
    logger.info("Starting MongoDB sink");
    sinkCounter.start();/*  ww w  .j  a va 2s . c om*/
    try {
        client = new MongoClient(seeds, Arrays.asList(credential));
        MongoDatabase database = client.getDatabase(databaseName);
        collection = database.getCollection(collectionName);
        sinkCounter.incrementConnectionCreatedCount();
    } catch (Exception e) {
        logger.error("Exception while connecting to MongoDB", e);
        sinkCounter.incrementConnectionFailedCount();
        if (client != null) {
            client.close();
            sinkCounter.incrementConnectionClosedCount();
        }
    }
    super.start();
    logger.info("MongoDB sink started");
}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

@Override
public Optional<T> find(String key, Object value) {
    try {//  w w  w  .j  a v  a2 s  .com

        //   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(Repository.class.getName() + "find()").log(Level.SEVERE, null, e);
                    exception = new Exception("find() ", e);
                }

            }
        });
        if (haveElements) {

            return Optional.of(tlocal);
        }
        return Optional.empty();

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("find() ", e);

    }

    return Optional.empty();
}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

public T search(String key, Object value) {
    try {//from  w  w w.ja  v a 2  s . c om

        MongoDatabase db = getMongoClient().getDatabase(database);

        if (db == null) {
            return null;
        } else {
        }
        FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value));
        //Test.msg("+++ paso iterable");
        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(Repository.class.getName() + "search()").log(Level.SEVERE, null, e);
                    exception = new Exception("search() ", e);
                }

            }
        });
        if (haveElements) {
            return tlocal;
        }
        return null;

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("search() ", e);

    }

    return null;

}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

public T search(String key, Integer value) {
    try {//from  w w w  .  jav  a  2  s .c  om

        MongoDatabase db = getMongoClient().getDatabase(database);

        if (db == null) {
            return null;
        } else {
        }
        FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value));
        //Test.msg("+++ paso iterable");
        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(Repository.class.getName() + "search()").log(Level.SEVERE, null, e);
                    exception = new Exception("search() ", e);
                }

            }
        });
        if (haveElements) {
            return tlocal;
        }
        return null;

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("search() ", e);

    }

    return null;

}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

public T search(String key, String value) {
    try {/*  w w  w .j  a v  a 2 s.c  om*/

        MongoDatabase db = getMongoClient().getDatabase(database);

        if (db == null) {
            return null;
        } else {
        }
        FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, value));
        //Test.msg("+++ paso iterable");
        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(Repository.class.getName() + "search()").log(Level.SEVERE, null, e);
                    exception = new Exception("search() ", e);
                }

            }
        });
        if (haveElements) {
            return tlocal;
        }
        return null;

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("search() ", e);

    }

    return null;

}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

/**
 *
 * @param document/* www. j  av  a2 s  .  c  om*/
 * @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(Repository.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.ejbjmoordb.mongodb.repository.Repository.java

/**
 * Devuelve el primer documento de la coleccion
 *
 * @param document//from w w  w .  j a va  2 s .  c  o  m
 * @return
 */
@Override
public Optional<T> findFirst(Document... doc) {
    try {
        Document document = new Document();
        MongoDatabase db = getMongoClient().getDatabase(database);
        if (doc.length != 0) {
            document = doc[0];

            FindIterable<Document> iterable = db.getCollection(collection).find(document).limit(1);
            tlocal = (T) iterableSimple(iterable);
            return Optional.of(tlocal);
        } else {

            FindIterable<Document> iterable = db.getCollection(collection).find().limit(1);
            tlocal = (T) iterableSimple(iterable);
            return Optional.of(tlocal);
        }

        //return (T) tlocal;
    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("find() ", e);
        new JmoordbException("find()");
    }
    return Optional.empty();
}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

/**
 * Devuelve el primer documento de la coleccion
 *
 * @param document/*from w  w  w.j a v  a  2 s  .  c om*/
 * @return
 */
public Optional<T> findFirst(String sql) {
    try {
        Document doc = new Document();
        Document sortQuery = new Document();
        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).limit(1);
        tlocal = (T) iterableSimple(iterable);
        return Optional.of(tlocal);

        //return (T) tlocal;
    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("find() ", e);
        new JmoordbException("find()");
    }
    return Optional.empty();
}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

private T findInternal(Document document) {
    try {//from ww  w  .  jav  a  2s  .  com
        //   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(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("find() ", e);
        new JmoordbException("find()");
    }
    return null;
}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

/**
 *
 * @param doc/*from   ww w.  ja  va  2 s.  co  m*/
 * @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(Repository.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(Repository.class.getName() + "count()").log(Level.SEVERE, null, e);
        exception = new Exception("count()", e);
    }
    return contador;
}