Example usage for com.mongodb.client.model Sorts orderBy

List of usage examples for com.mongodb.client.model Sorts orderBy

Introduction

In this page you can find the example usage for com.mongodb.client.model Sorts orderBy.

Prototype

private static Bson orderBy(final List<String> fieldNames, final BsonValue value) 

Source Link

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,
        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 -> {//from  w  ww. j a va  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> 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 -> {//from  w w w . java2  s  . c o  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;
}