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

public static Bson orderBy(final List<? extends Bson> sorts) 

Source Link

Document

Combine multiple sort specifications.

Usage

From source file:com.egopulse.querydsl.mongodb.MongodbSerializer.java

License:Apache License

public Bson toSort(List<OrderSpecifier<?>> orderBys) {
    List<Bson> bsons = orderBys.stream().map(orderBy -> {
        Object key = orderBy.getTarget().accept(this, null);
        return orderBy.getOrder() == Order.ASC ? Sorts.ascending((String) key) : Sorts.descending((String) key);
    }).collect(Collectors.toList());
    return Sorts.orderBy(bsons);
}

From source file:com.github.cherimojava.data.mongo.query.QueryInvocationHandler.java

License:Apache License

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    String methodName = method.getName();
    switch (methodName) {
    case "e":
        // this is just a handout for the entity to get knowledge of what to check
        return this.entityProxy.get();
    case "where":/* fallthrough */
    case "and":
        return this.specifier.get();
    case "iterator":
        FindIterable it = coll.find(Filters.and(filters.toArray(new Bson[] {})));
        if (limit != null) {
            it.limit(limit);/*from  w  w  w .ja v a 2  s . c  o m*/
        }
        if (skip != null) {
            it.skip(skip);
        }
        if (sorts.size() > 0) {
            it.sort(Sorts.orderBy(sorts));
        }
        return it.iterator();
    case "count":
        return coll.count(Filters.and(filters.toArray(new Bson[] {})));
    case "limit":
        limit = (Integer) args[0];
        return queryEnd.get();
    case "skip":
        skip = (Integer) args[0];
        return queryEnd.get();
    case "sort":
        checkState(!sortSet, "Sorting can be specified only once");
        sortSet = true;
        return querySort.get();
    case "desc":/* fallthrough */
    case "asc":
        return addSortInformation("asc".equals(methodName));
    case "by":
        return addSortInformation(args[0] == QuerySort.Sort.ASC);
    }
    throw new IllegalStateException("Unknown method found: " + methodName);
}

From source file:com.px100systems.data.plugin.storage.mongo.MongoDatabaseStorage.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T> List<T> search(String unitName, Class<T> cls, Criteria criteria, List<String> orderBy,
        Integer limit) {//from   ww  w  .  ja va 2s  .  c o m
    SerializationDefinition def = SerializationDefinition.get(cls);
    if (def == null)
        throw new RuntimeException("Cannot find SerializationDefinition for " + cls.getSimpleName());

    MongoDatabase db = mongoClient.getDatabase(databaseName);

    FindIterable<Document> query = criteria == null ? db.getCollection(unitName).find()
            : db.getCollection(unitName).find(criteria.convert(new FilterQueryBuilder()));

    if (orderBy != null && !orderBy.isEmpty())
        if (orderBy.size() == 1)
            query = query.sort(orderBy(orderBy.get(0)));
        else {
            List<Bson> ob = new ArrayList<>();
            for (String s : orderBy)
                ob.add(orderBy(s));
            query = query.sort(Sorts.orderBy(ob));
        }

    List<T> result = new ArrayList<>();
    MongoCursor<Document> cursor = query.limit(limit).iterator();
    try {
        while (cursor.hasNext()) {
            T item = (T) def.newInstance();
            def.read(cursor.next(), item);
            result.add(item);
        }
    } finally {
        cursor.close();
    }

    return result;
}

From source file:com.px100systems.data.plugin.storage.mongo.MongoDatabaseStorage.java

License:Open Source License

@Override
public <T> EntityCursor<T> search(String unitName, Class<T> cls, Criteria criteria, List<String> orderBy) {
    SerializationDefinition def = SerializationDefinition.get(cls);
    if (def == null)
        throw new RuntimeException("Cannot find SerializationDefinition for " + cls.getSimpleName());

    MongoDatabase db = mongoClient.getDatabase(databaseName);

    FindIterable<Document> query = criteria == null ? db.getCollection(unitName).find()
            : db.getCollection(unitName).find(criteria.convert(new FilterQueryBuilder()));

    if (orderBy != null && !orderBy.isEmpty())
        if (orderBy.size() == 1)
            query = query.sort(orderBy(orderBy.get(0)));
        else {/*w  ww.j  av  a  2 s  .co m*/
            List<Bson> ob = new ArrayList<>();
            for (String s : orderBy)
                ob.add(orderBy(s));
            query = query.sort(Sorts.orderBy(ob));
        }

    return new ResultIterator<T>(query.iterator(), def);
}

From source file:dto.Dto.java

public String getLastStudentId() {
    Conexion c = new Conexion();
    MongoCollection<Document> col = c.getConnection("usuarios");

    Document doc = col.find().sort(Sorts.orderBy(Sorts.descending("_id"))).first();

    if (doc == null) {
        return "0";
    } else {//from ww w .j a  v a  2 s.  c o  m
        return (doc.getString("_id"));
    }
}

From source file:dto.Dto.java

public String getLastActaId() {
    Conexion c = new Conexion();
    MongoCollection<Document> col = c.getConnection("actas");

    Document doc = col.find().sort(Sorts.orderBy(Sorts.descending("_id"))).first();

    if (doc == null) {
        return "0";
    } else {//from  www . ja  va 2 s.co m
        return (doc.getString("_id"));
    }
}

From source file:dto.Dto.java

public String getLastAsesoriaId() {
    Conexion c = new Conexion();
    MongoCollection<Document> col = c.getConnection("asesorias");

    Document doc = col.find().sort(Sorts.orderBy(Sorts.descending("_id"))).first();

    if (doc == null) {
        return "0";
    } else {/*from   w  ww .j  a va2 s.com*/
        return (doc.getString("_id"));
    }
}

From source file:dto.Dto.java

public String getLastTeacherId() {
    Conexion c = new Conexion();
    MongoCollection<Document> col = c.getConnection("profesores");

    Document doc = col.find().sort(Sorts.orderBy(Sorts.descending("_id"))).first();

    if (doc == null) {
        return "0";
    } else {//from w w  w  .  ja v a  2s. c om
        return (doc.getString("_id"));
    }
}

From source file:dto.Dto.java

public String getLastTesisId() {
    Conexion c = new Conexion();
    MongoCollection<Document> col = c.getConnection("tesis_alumno_asesor");

    Document doc = col.find().sort(Sorts.orderBy(Sorts.descending("_id"))).first();

    if (doc == null) {
        return "0";
    } else {//  www  . j av a 2  s  .co  m
        System.out.println(doc.getString("_id"));
        return (doc.getString("_id"));

    }
}

From source file:dto.Dto.java

public String listarTesis() {
    String cadena = "";
    MongoCollection<Document> col = c.getConnection("universo_tesis");
    MongoCursor<Document> cursor = col.find().sort(Sorts.orderBy(Sorts.descending("ao"))).iterator();
    Document doc;/*  w  w  w . j a va 2s . c  o m*/
    try {
        while (cursor.hasNext()) {
            doc = cursor.next();
            cadena += "<tr>" + "<td width='20%'>" + doc.getString("titulo").toUpperCase().trim() + "</td>"
                    + "<td width='20%'>" + doc.getString("ao") + "</td>" + "<td width='20%'>"
                    + doc.getString("estado").toUpperCase().trim() + "</td>" + "</tr>";
        }
    } catch (Exception e) {
        System.out.println("listarTesis universo: " + e);
    } finally {
        cursor.close();
    }
    return cadena;
}