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 <T extends JaqpotEntity> List<T> findSorted(Class<T> entityClass, Map<String, Object> properties,
        List<String> fields, 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 -> {/*w w  w . j a  va  2s  .co m*/
        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)
            .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,
        List<String> fields, 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 -> {//from   w  w w  .  j a va  2s  .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).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,
        List<String> fields, 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.  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).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> List<T> findAll(Class<T> entityClass, Integer start, Integer max) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    List<T> result = new ArrayList<>();
    collection.find().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> findAll(Class<T> entityClass, List<String> fields, Integer start,
        Integer max) {//from w  w w. j a  v  a 2 s .  c o m
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    List<T> result = new ArrayList<>();
    Document filter = new Document();
    fields.stream().forEach(f -> filter.put(f, 1));
    collection.find().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;
}

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

License:Open Source License

@Override
public <T extends JaqpotEntity> Long countAll(Class<T> entityClass) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    return collection.count();
}

From source file:org.jboss.as.quickstarts.kitchensink.util.Resources.java

License:Apache License

@Produces
private MongoCollection<Document> produceMongoCollection(MongoDatabase mongoDatabase) {
    return mongoDatabase.getCollection(Member.class.getSimpleName());
}

From source file:org.jooby.mongodb.MongoSessionStore.java

License:Apache License

public MongoSessionStore(final MongoDatabase db, final String collection, final long timeoutInSeconds) {
    this.db = requireNonNull(db, "Mongo db is required.");
    this.collection = requireNonNull(collection, "Collection is required.");
    this.sessions = db.getCollection(collection);
    this.timeout = timeoutInSeconds;
}

From source file:org.juanitodread.ecommercerest.model.dao.imp.MongoCustomerDaoImp.java

License:Apache License

public MongoCustomerDaoImp(final MongoDatabase customersDatabase) {
    customersCollection = customersDatabase.getCollection(COLLECTION);
}

From source file:org.juanitodread.ecommercerest.model.dao.imp.MongoOrderDaoImp.java

License:Apache License

public MongoOrderDaoImp(final MongoDatabase ordersDatabase) {
    ordersCollection = ordersDatabase.getCollection(COLLECTION);
}