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.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

/**
 *
 * @param filter/*from w  w  w  . j a  v  a2 s.  c  o  m*/
 * @param docSort
 * @return
 */
public List<T> filters(Bson filter, 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(filter).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

/**
 *
 * @param key/*from w  w  w .  j a v  a  2 s.c om*/
 * @param value
 * @param docSort
 * @return
 */
public List<T> findlike(String key, String value, Document... docSort) {
    Document sortQuery = new Document();
    list = new ArrayList<>();

    try {

        if (docSort.length != 0) {
            sortQuery = docSort[0];

        }
        Object t = entityClass.newInstance();
        Pattern regex = Pattern.compile(value);

        MongoDatabase db = getMongoClient().getDatabase(database);
        FindIterable<Document> iterable = db.getCollection(collection).find(new Document(key, regex))
                .sort(sortQuery);
        list = iterableList(iterable);

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("findLike()", e);
    }
    return list;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

public List<T> findHelperSort(String predicate, Document doc, String key, String value) {
    try {/*from   w  ww  .j  ava  2s. c  om*/

        Object t = entityClass.newInstance();
        list = new ArrayList<>();

        MongoDatabase db = getMongoClient().getDatabase(database);
        FindIterable<Document> iterable = getIterable();
        switch (predicate) {
        case "ascending":
            iterable = db.getCollection(collection).find(doc).sort(ascending(key, value));
            break;
        case "descending":
            iterable = db.getCollection(collection).find(doc).sort(descending(key, value));
            break;

        }

        list = iterableList(iterable);

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("findHelperSort()", e);
    }
    return list;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

/**
 *
 * @param predicate eq,gt.lt//w w  w.  jav a2  s . co  m
 * @param key
 * @param value
 * @param docSort
 * @return
 */
public List<T> helpers(String predicate, String key, Object value, Document... docSort) {
    Document sortQuery = new Document();
    try {
        if (docSort.length != 0) {
            sortQuery = docSort[0];

        }
        Object t = entityClass.newInstance();
        list = new ArrayList<>();

        MongoDatabase db = getMongoClient().getDatabase(database);
        FindIterable<Document> iterable = getIterable();
        switch (predicate) {
        case "eq":
            iterable = db.getCollection(collection).find(eq(key, value)).sort(sortQuery);
            break;
        case "lt":
            iterable = db.getCollection(collection).find(lt(key, value)).sort(sortQuery);
            break;
        case "gt":
            iterable = db.getCollection(collection).find(gt(key, value)).sort(sortQuery);
            break;
        }

        list = iterableList(iterable);

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("helpers()", e);
    }
    return list;
}

From source file:com.averageloser.mongodemo.Model.DBHelper.java

public MongoCollection<Document> getMongoCollection(MongoDatabase database, String name) {
    return database.getCollection(name);
}

From source file:com.bdnc.ecommercebdnc.dao.DAODocumentos.java

public boolean salvarCompra(Compra compra) {
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase dataBase = client.getDatabase("ecommerce-bdnc");
    MongoCollection<Document> collection = dataBase.getCollection("vendas");
    collection.insertOne(compra.toDocument());
    client.close();/*from   w w w  .  ja va  2s.  co m*/
    return true;
}

From source file:com.bdnc.ecommercebdnc.dao.DAODocumentos.java

public List<Compra> buscarCompras() {
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase dataBase = client.getDatabase("ecommerce-bdnc");
    MongoCollection<Document> collection = dataBase.getCollection("vendas");
    List<Compra> compras = new ArrayList<>();
    MongoCursor<Document> cursor = collection.find().iterator();
    while (cursor.hasNext()) {
        Compra compra = new Compra();
        compras.add(compra.fromDocument(cursor.next()));
    }// w w w  .j a v  a 2  s.  c  om
    client.close();
    return compras;
}

From source file:com.bilko.dao.SessionDao.java

License:Apache License

public SessionDao(final MongoDatabase db) {
    collection = db.getCollection("sessions");
}

From source file:com.bilko.dao.UserDao.java

License:Apache License

public UserDao(final MongoDatabase db) {
    collection = db.getCollection("users");
}

From source file:com.bluedragon.mongo.MongoCollectionAggregate.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    cfData data = getNamedParam(argStruct, "pipeline", null);
    if (data == null)
        throwException(_session, "please specify a pipeline");

    List<Bson> pipelineList = new ArrayList<Bson>();

    if (data.getDataType() == cfData.CFARRAYDATA) {
        cfArrayData arrData = (cfArrayData) data;

        if (arrData.size() == 0)
            throwException(_session, "please specify at least one pipeline");

        for (int x = 0; x < arrData.size(); x++)
            pipelineList.add(getDocument(arrData.getData(x + 1)));

    } else {//w  w  w  .j a v  a  2 s.  c om
        pipelineList.add(getDocument(data));
    }

    try {
        MongoCollection<Document> col = db.getCollection(collection);
        long start = System.currentTimeMillis();

        // Now we can run the query
        cfArrayData results = cfArrayData.createArray(1);

        col.aggregate(pipelineList).forEach(new Block<Document>() {

            @SuppressWarnings("rawtypes")
            @Override
            public void apply(Document doc) {
                try {
                    results.addElement(tagUtils.convertToCfData((Map) doc));
                } catch (cfmRunTimeException e) {
                }
            }

        });

        _session.getDebugRecorder().execMongo(col, "aggregate", null, System.currentTimeMillis() - start);

        return results;
    } catch (Exception e) {
        throwException(_session, e.getMessage());
        return null;
    }
}