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:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public void remove(JaqpotEntity entity) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entity.getClass()));
    collection.deleteOne(new Document("_id", entity.getId()));
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> T find(Class<T> entityClass, Object primaryKey) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    Document retrieved = collection.find(new Document("_id", primaryKey)).first();
    return serializer.parse(JSON.serialize(retrieved), entityClass);
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> T find(Class<T> entityClass, Object primaryKey, List<String> fields) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    Document retrieved = collection.find(new Document("_id", primaryKey)).projection(include(fields)).first();
    return serializer.parse(JSON.serialize(retrieved), entityClass);
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> List<T> find(Class<T> entityClass, Map<String, Object> properties,
        Integer start, Integer max) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    properties.entrySet().stream().filter(e -> {
        return e.getValue() instanceof List;
    }).forEach(e -> {//from  w w w. j av  a2 s .c om
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    List<T> result = new ArrayList<>();
    collection.find(new Document(properties)).skip(start != null ? start : 0)
            .limit(max != null ? max : DEFAULT_PAGE_SIZE)
            .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result);
    return result;
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> List<T> findSorted(Class<T> entityClass, Map<String, Object> properties,
        Integer start, Integer max, List<String> ascendingFields, List<String> descendingFields) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    properties.entrySet().stream().filter(e -> {
        return e.getValue() instanceof List;
    }).forEach(e -> {//  ww w  . j a  v  a  2s. co  m
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    List<T> result = new ArrayList<>();
    collection.find(new Document(properties))
            .sort(Sorts.orderBy(Sorts.ascending(ascendingFields), Sorts.descending(descendingFields)))
            .skip(start != null ? start : 0).limit(max != null ? max : DEFAULT_PAGE_SIZE)
            .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result);
    return result;
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> List<T> findSortedAsc(Class<T> entityClass, Map<String, Object> properties,
        Integer start, Integer max, List<String> ascendingFields) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    properties.entrySet().stream().filter(e -> {
        return e.getValue() instanceof List;
    }).forEach(e -> {//  w w  w . j  a  va 2  s . co  m
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    List<T> result = new ArrayList<>();
    collection.find(new Document(properties)).sort(Sorts.ascending(ascendingFields))
            .skip(start != null ? start : 0).limit(max != null ? max : DEFAULT_PAGE_SIZE)
            .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result);
    return result;
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> List<T> findSortedDesc(Class<T> entityClass, Map<String, Object> properties,
        Integer start, Integer max, List<String> descendingFields) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    properties.entrySet().stream().filter(e -> {
        return e.getValue() instanceof List;
    }).forEach(e -> {//from   w  ww  . j av  a 2 s.co  m
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    List<T> result = new ArrayList<>();
    collection.find(new Document(properties)).sort(Sorts.descending(descendingFields))
            .skip(start != null ? start : 0).limit(max != null ? max : DEFAULT_PAGE_SIZE)
            .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result);
    return result;
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> Long count(Class<T> entityClass, Map<String, Object> properties) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    properties.entrySet().stream().filter(e -> {
        return e.getValue() instanceof List;
    }).forEach(e -> {//from ww  w  .j a  v a 2s .  c om
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });
    return collection.count(new Document(properties));
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> List<T> find(Class<T> entityClass, List<String> keys, List<String> fields) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    List<T> result = new ArrayList<>();
    Document query = new Document("_id", new Document("$in", keys));
    Document filter = new Document();
    fields.stream().forEach(f -> filter.put(f, 1));
    collection.find(query).projection(filter)
            .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result);
    return result;
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> List<T> find(Class<T> entityClass, Map<String, Object> properties,
        List<String> fields, Integer start, Integer max) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    properties.entrySet().stream().filter(e -> {
        return e.getValue() instanceof List;
    }).forEach(e -> {//  w  w  w .  ja  v  a 2 s. c om
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    Document filter = new Document();
    fields.stream().forEach(f -> filter.put(f, 1));
    List<T> result = new ArrayList<>();
    collection.find(new Document(properties)).projection(filter).skip(start != null ? start : 0)
            .limit(max != null ? max : DEFAULT_PAGE_SIZE)
            .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result);
    return result;
}