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.ejbjmoordb.mongodb.repository.Repository.java

/**
 *
 * @param predicate eq,gt.lt//from   ww w .  jav a  2s  .  co m
 * @param key
 * @param value
 * @param docSort
 * @return
 */
public List<T> helpersPagination(String predicate, String key, Object value, Integer pageNumber,
        Integer rowsForPage, 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))
                    .skip(pageNumber > 0 ? ((pageNumber - 1) * rowsForPage) : 0).limit(rowsForPage)
                    .sort(sortQuery);
            break;
        case "lt":
            iterable = db.getCollection(collection).find(lt(key, value))
                    .skip(pageNumber > 0 ? ((pageNumber - 1) * rowsForPage) : 0).limit(rowsForPage)
                    .sort(sortQuery);
            break;
        case "gt":
            iterable = db.getCollection(collection).find(gt(key, value))
                    .skip(pageNumber > 0 ? ((pageNumber - 1) * rowsForPage) : 0).limit(rowsForPage)
                    .sort(sortQuery);
            break;
        }

        list = iterableList(iterable);

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

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

/**
 * Requiere que se cree un indice primero
 * URL:https://docs.mongodb.com/manual/reference/operator/query/text/
 * Indice: db.planetas.createIndex( { idplaneta: "text" } )
 *
 * @param key//w  w w.  j a  v a2s.  c o m
 * @param value
 * @param caseSensitive = true
 * @param diacriticSensitive = true
 * @param docSort
 * @return
 */
public List<T> findText(String key, String value, Boolean caseSensitive, Boolean diacriticSensitive,
        Document... docSort) {
    Document sortQuery = new Document();
    list = new ArrayList<>();

    try {

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

        }
        Object t = entityClass.newInstance();
        MongoDatabase db = getMongoClient().getDatabase(database);
        FindIterable<Document> iterable = db.getCollection(collection).find(
                new Document("$text", new Document("$search", value).append("$caseSensitive", caseSensitive)
                        .append("$diacriticSensitive", diacriticSensitive)));

        list = iterableList(iterable);

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

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

/**
 * Requiere que se cree un indice primero
 * URL:https://docs.mongodb.com/manual/reference/operator/query/text/
 * Indice: db.planetas.createIndex( { idplaneta: "text" } )
 *
 * @param key// w  ww.  j a va2  s.co m
 * @param value
 * @param caseSensitive = true
 * @param diacriticSensitive = true
 * @param docSort
 * @return
 */
public List<T> findTextPagination(String key, String value, Boolean caseSensitive, Boolean diacriticSensitive,
        Integer pageNumber, Integer rowsForPage, Document... docSort) {
    Document sortQuery = new Document();
    list = new ArrayList<>();

    try {

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

        }
        Object t = entityClass.newInstance();
        MongoDatabase db = getMongoClient().getDatabase(database);
        FindIterable<Document> iterable = db.getCollection(collection)
                .find(new Document("$text",
                        new Document("$search", value).append("$caseSensitive", caseSensitive)
                                .append("$diacriticSensitive", diacriticSensitive)))
                .skip(pageNumber > 0 ? ((pageNumber - 1) * rowsForPage) : 0).limit(rowsForPage);

        list = iterableList(iterable);

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

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

/**
 *
 * @param key/*  www  . j ava  2s.  c  o m*/
 * @param value
 * @param docSort
 * @return
 */
public List<T> findRegex(String key, String value, Boolean caseSensitive, 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;
        if (!caseSensitive) {
            iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", "^" + value)))
                    .sort(sortQuery);
            //iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", regex)));
        } else {
            iterable = db.getCollection(collection)
                    .find(new Document(key, new Document("$regex", "^" + value).append("$options", "i")))
                    .sort(sortQuery);
            //               iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", regex).append("$options", "si")));

        }

        list = iterableList(iterable);

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

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

/**
 *
 * @param key/*from ww  w .j a  v  a 2 s  .  c  o  m*/
 * @param value
 * @param docSort
 * @return
 */
public List<T> findRegex(String key, String value, Boolean caseSensitive, String keySecond, String valueSecond,
        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;
        if (!caseSensitive) {
            iterable = db.getCollection(collection)
                    .find(new Document(key, new Document("$regex", "^" + value)).append(keySecond, valueSecond))
                    .sort(sortQuery);
            //iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", regex)));
        } else {
            iterable = db.getCollection(collection)
                    .find(new Document(key, new Document("$regex", "^" + value).append("$options", "i"))
                            .append(keySecond, valueSecond))
                    .sort(sortQuery);
            //               iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", regex).append("$options", "si")));

        }

        list = iterableList(iterable);

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

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

/**
 *
 * @param key//from   w  w w .  jav  a2 s.com
 * @param value
 * @param docSort
 * @return
 */
public List<T> findRegex(String key, String value, Boolean caseSensitive, String keySecond, Object valueSecond,
        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;
        if (!caseSensitive) {
            switch (typeOfObject(valueSecond)) {
            case "String":
                iterable = db.getCollection(collection)
                        .find(new Document(key, new Document("$regex", "^" + value)).append(keySecond,
                                valueSecond.toString()))
                        .sort(sortQuery);
                break;
            case "Integer":
                iterable = db.getCollection(collection)
                        .find(new Document(key, new Document("$regex", "^" + value)).append(keySecond,
                                Integer.parseInt(valueSecond.toString())))
                        .sort(sortQuery);
                break;

            case "Double":
                iterable = db.getCollection(collection)
                        .find(new Document(key, new Document("$regex", "^" + value)).append(keySecond,
                                Double.parseDouble(valueSecond.toString())))
                        .sort(sortQuery);
                break;
            case "Date":
                iterable = db.getCollection(collection)
                        .find(new Document(key, new Document("$regex", "^" + value)).append(keySecond,
                                new Date(valueSecond.toString())))
                        .sort(sortQuery);
                break;
            default:
                iterable = db.getCollection(collection)
                        .find(new Document(key, new Document("$regex", "^" + value)).append(keySecond,
                                valueSecond.toString()))
                        .sort(sortQuery);
                break;

            }

        } else {
            //                iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", "^" + value).append("$options", "i")).append(keySecond, valueSecond)).sort(sortQuery);
            switch (typeOfObject(valueSecond)) {
            case "String":
                iterable = db.getCollection(collection)
                        .find(new Document(key, new Document("$regex", "^" + value).append("$options", "i"))
                                .append(keySecond, valueSecond.toString()))
                        .sort(sortQuery);
                break;
            case "Integer":
                iterable = db.getCollection(collection)
                        .find(new Document(key, new Document("$regex", "^" + value).append("$options", "i"))
                                .append(keySecond, Integer.parseInt(valueSecond.toString())))
                        .sort(sortQuery);
                break;

            case "Double":
                iterable = db.getCollection(collection)
                        .find(new Document(key, new Document("$regex", "^" + value).append("$options", "i"))
                                .append(keySecond, Double.parseDouble(valueSecond.toString())))
                        .sort(sortQuery);
                break;
            case "Date":
                iterable = db.getCollection(collection)
                        .find(new Document(key, new Document("$regex", "^" + value).append("$options", "i"))
                                .append(keySecond, new Date(valueSecond.toString())))
                        .sort(sortQuery);
                break;
            default:
                iterable = db.getCollection(collection)
                        .find(new Document(key, new Document("$regex", "^" + value).append("$options", "i"))
                                .append(keySecond, valueSecond.toString()))
                        .sort(sortQuery);

                break;

            }

        }

        list = iterableList(iterable);

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

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

/**
 *
 * @param key/* www. j  av  a 2 s  . co m*/
 * @param value
 * @param docSort
 * @return
 */
public List<T> findRegex(String key, String value, Boolean caseSensitive, String keySecond, String valueSecond,
        String keyThree, String valueTree, 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;
        if (!caseSensitive) {
            iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", "^" + value))
                    .append(keySecond, valueSecond).append(keyThree, valueTree)).sort(sortQuery);
            //iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", regex)));
        } else {
            iterable = db.getCollection(collection)
                    .find(new Document(key, new Document("$regex", "^" + value).append("$options", "i"))
                            .append(keySecond, valueSecond).append(keyThree, valueTree))
                    .sort(sortQuery);
            //               iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", regex).append("$options", "si")));

        }

        list = iterableList(iterable);

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

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

/**
 *
 * @param key// ww  w. j  a  v  a2  s . c o m
 * @param value
 * @param docSort
 * @return
 */
public List<T> findRegexInText(String key, String value, Boolean caseSensitive, 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;
        if (!caseSensitive) {
            iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", value)))
                    .sort(sortQuery);
        } else {
            iterable = db.getCollection(collection)
                    .find(new Document(key, new Document("$regex", value).append("$options", "i")))
                    .sort(sortQuery);

        }

        list = iterableList(iterable);

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

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

/**
 *
 * @param key/*from  ww w .j  a v  a 2  s. c o m*/
 * @param value
 * @param docSort
 * @return
 */
public List<T> findRegexInText(String key, String value, Boolean caseSensitive, String keySecond,
        String valueSecond, 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;
        if (!caseSensitive) {
            iterable = db.getCollection(collection)
                    .find(new Document(key, new Document("$regex", value)).append(keySecond, valueSecond))
                    .sort(sortQuery);

        } else {
            iterable = db.getCollection(collection)
                    .find(new Document(key, new Document("$regex", value).append("$options", "i"))
                            .append(keySecond, valueSecond))
                    .sort(sortQuery);

        }

        list = iterableList(iterable);

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

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

/**
 *
 * @param key/*from   w w  w  .  j  a v a  2 s. co m*/
 * @param value
 * @param docSort
 * @return
 */
public List<T> findRegexInText(String key, String value, Boolean caseSensitive, String keySecond,
        String valueSecond, String keyThree, String valueThree, 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;
        if (!caseSensitive) {
            iterable = db.getCollection(collection).find(new Document(key, new Document("$regex", value))
                    .append(keySecond, valueSecond).append(keyThree, valueThree)).sort(sortQuery);

        } else {
            iterable = db.getCollection(collection)
                    .find(new Document(key, new Document("$regex", value).append("$options", "i"))
                            .append(keySecond, valueSecond).append(keyThree, valueThree))
                    .sort(sortQuery);

        }

        list = iterableList(iterable);

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