Example usage for com.mongodb DBCursor sort

List of usage examples for com.mongodb DBCursor sort

Introduction

In this page you can find the example usage for com.mongodb DBCursor sort.

Prototype

public DBCursor sort(final DBObject orderBy) 

Source Link

Document

Sorts this cursor's elements.

Usage

From source file:com.ff.reportgenerator.mongodb.DynamicDatabase.java

public String query() {
    String records = "<table>" + Utility.formTableHead() + "<tbody>";

    DB myDB = getDB(DB_NAME);/*from w w w.  j a  va 2  s  . c  o m*/
    DBCollection coll = myDB.getCollection("projects");

    DBCursor ret = coll.find();
    BasicDBObject sort = new BasicDBObject("PROJECT_ID", 1);
    ret.sort(sort);

    try {
        while (ret.hasNext()) {
            records = records + "<tr>";

            DBObject rec = (DBObject) ret.next();
            Iterator keys = Utility.DATA_KEYS.iterator();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                String value = (String) rec.get(key);
                if (key.equals("PROJECT_ID")) {
                    records = records + "<td><a href=\"" + Utility.PROJECT_URL_PREFIX + value + "\">" + value
                            + "</td>";
                } else {
                    records = records + "<td>" + value + "</td>";

                }
            }

            records = records + "</tr>";
            //System.out.println(rec);
        }
    } finally {
        ret.close();
    }

    records = records + "</tbody></table>";
    return records;
}

From source file:com.ff.reportgenerator.mongodb.DynamicDatabase.java

public String query(String condition) {
    List<DBObject> list = null;

    DB myDB = getDB(DB_NAME);/*from   w w w .j a  va  2s .c om*/
    DBCollection coll = myDB.getCollection("projects");

    DBCursor ret = coll.find();
    BasicDBObject sort = new BasicDBObject("PROJECT_ID", 1);
    ret.sort(sort);

    try {
        list = ret.toArray();
    } finally {
        ret.close();
    }

    System.out.println("quering...." + condition);
    return list.toString();
}

From source file:com.ff.reportgenerator.mongodb.DynamicDatabase.java

public String query(Hashtable conditions) {
    List<DBObject> list = null;

    DB myDB = getDB(DB_NAME);//w  ww . j  av  a 2  s  .  c o  m
    DBCollection coll = myDB.getCollection("projects");

    BasicDBObject cond = new BasicDBObject();

    Set<String> keySet = conditions.keySet();
    Iterator<String> it = keySet.iterator();

    while (it.hasNext()) {
        String key = it.next();
        String value = (String) conditions.get(key);
        if (value == null || value.equals("All")) {
            continue;
        }

        Pattern pattern = null;
        if (key.equals("Project_Phase")) {
            if (value.equals("Ongoing")) {
                BasicDBList dlist = new BasicDBList();
                dlist.add(new BasicDBObject(key, new BasicDBObject("$ne", "Completed")));
                dlist.add(new BasicDBObject(key, new BasicDBObject("$ne", "Eng-Complete")));
                dlist.add(new BasicDBObject(key, new BasicDBObject("$ne", "Canceled")));
                dlist.add(new BasicDBObject(key, new BasicDBObject("$ne", "Publication")));
                cond.append("$and", dlist);
            } else if (value.equals("Complete")) {
                ArrayList<String> slist = new ArrayList();
                slist.add("Completed");
                slist.add("Eng-Complete");
                slist.add("Publication");
                cond.append(key, new BasicDBObject("$in", slist));

                //Limited to FY15 projects
                //pattern = Pattern.compile("^.*15-\\[.*$", Pattern.CASE_INSENSITIVE);

                //Limited to FY16 projects
                pattern = Pattern.compile("^.*16-.*$", Pattern.CASE_INSENSITIVE);
                cond.append("ISVe_Goal", pattern);
            }
        } else if (key.equals("Keywords") && value.equals("Non-OPI")) { // not like 'OPI'
            pattern = Pattern.compile("^((?!OPI).)*$", Pattern.CASE_INSENSITIVE);
            cond.append(key, pattern);
        } else { // like '*OPI*'
            pattern = Pattern.compile("^.*" + value + ".*$", Pattern.CASE_INSENSITIVE);
            cond.append(key, pattern);
        }

        System.out.println(key + ":" + value);
    }

    DBCursor ret = coll.find(cond);

    BasicDBObject sort = new BasicDBObject("PROJECT_ID", 1);
    ret.sort(sort);

    try {
        list = ret.toArray();
    } finally {
        ret.close();
    }

    //System.out.println(list.toString());
    return list.toString();
}

From source file:com.groupon.jenkins.mongo.MongoRepository.java

License:Open Source License

protected <T> Iterable<T> find(DBObject query, DBObject fields, DBObject sort, Integer limit,
        Function<DBObject, T> transformer) {
    MongoClient client = getClient();//from   w  w  w . ja va  2s . c o  m
    try {
        DBCursor cursor = fields == null ? getCollection(client).find(query)
                : getCollection(client).find(query, fields);
        if (sort != null) {
            cursor = cursor.sort(sort);
        }
        if (limit != null) {
            cursor = cursor.limit(limit);
        }
        List<T> result = new LinkedList<T>();

        try {
            while (cursor.hasNext()) {
                result.add(transformer.apply(cursor.next()));
            }
        } finally {
            cursor.close();
        }

        return result;
    } finally {
        client.close();
    }
}

From source file:com.heisenberg.mongo.MongoWorkflowStore.java

License:Apache License

@Override
public List<WorkflowImpl> loadWorkflows(WorkflowQueryImpl query) {
    BasicDBObject q = new BasicDBObject();
    if (query.id != null) {
        q.append(fields._id, new ObjectId(query.id));
    }//from  w  w  w .j  a  va  2  s.c  o m
    if (query.name != null) {
        q.append(fields.name, query.name);
    }
    List<WorkflowImpl> processes = new ArrayList<WorkflowImpl>();
    DBCursor cursor = find(q);
    if (query.limit != null) {
        cursor.limit(query.limit);
    }
    if (query.orderBy != null) {
        cursor.sort(writeOrderBy(query.orderBy));
    }
    while (cursor.hasNext()) {
        BasicDBObject dbProcess = (BasicDBObject) cursor.next();
        WorkflowImpl processDefinition = readProcessDefinition(dbProcess);
        processes.add(processDefinition);
    }
    return processes;
}

From source file:com.impetus.client.mongodb.query.gfs.KunderaGridFS.java

License:Apache License

/**
 * Finds a list of files matching the given query.
 * //from  ww w  .  j av  a2s . c om
 * @param query
 *            the filter to apply
 * @param sort
 *            the fields to sort with
 * @param firstResult
 *            number of files to skip
 * @param maxResult
 *            number of files to return
 * @return list of gridfs files
 */
public List<GridFSDBFile> find(final DBObject query, final DBObject sort, final int firstResult,
        final int maxResult) {
    List<GridFSDBFile> files = new ArrayList<GridFSDBFile>();

    DBCursor c = null;
    try {
        c = _filesCollection.find(query);
        if (sort != null) {
            c.sort(sort);
        }
        c.skip(firstResult).limit(maxResult);
        while (c.hasNext()) {
            files.add(_fix(c.next()));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return files;
}

From source file:com.janeluo.jfinalplus.plugin.monogodb.MongoKit.java

License:Apache License

private static void sort(Map<String, Object> sort, DBCursor dbCursor) {
    if (sort != null) {
        DBObject dbo = new BasicDBObject();
        Set<Entry<String, Object>> entrySet = sort.entrySet();
        for (Entry<String, Object> entry : entrySet) {
            String key = entry.getKey();
            Object val = entry.getValue();
            dbo.put(key, "asc".equalsIgnoreCase(val + "") ? 1 : -1);
        }/*from  w  w w.jav a 2s  .c  om*/
        dbCursor = dbCursor.sort(dbo);
    }
}

From source file:com.kurniakue.trxreader.data.TransactionD.java

public List<Transaction> getTransactionsNonRkapByDate(String dateStr) {
    List<Transaction> list = new ArrayList<>();
    if (dateStr == null) {
        return list;
    }//from  w  ww. j  a v a2 s. c o m
    DBCursor cursor;
    if ("".equals(dateStr)) {
        cursor = transactions.find();
    } else {
        cursor = transactions.find(new BasicDBObject(F.Date.name(), dateStr).append(F.ItemNo.name(),
                new BasicDBObject("$ne", "RKAP")));
    }
    cursor.sort(new BasicDBObject(F.ItemNo.name(), 1).append(F.TransactionID.name(), 1));

    while (cursor.hasNext()) {
        DBObject dbobject = cursor.next();
        Transaction transaction = new Transaction();
        transaction.putAll(dbobject.toMap());
        list.add(transaction); //filter by itemno and item name RKAP - saldo, fix amount to + instead of -
    }

    return list;
}

From source file:com.linuxbox.enkive.workspace.mongo.MongoSearchResultUtils.java

License:Open Source License

protected LinkedHashSet<String> sortMessages(Set<String> messageIds, String sortField, int sortDirection) {
    LinkedHashSet<String> sortedIds = new LinkedHashSet<String>();
    // Only want to return the ids
    BasicDBObject keys = new BasicDBObject();
    keys.put("_id", 1);
    keys.put(sortField, 1);/*from  w w  w .  j av  a  2 s .  c  om*/

    BasicDBObject query = new BasicDBObject();
    // Build object with IDs
    BasicDBList idList = new BasicDBList();
    idList.addAll(messageIds);
    BasicDBObject idQuery = new BasicDBObject();
    idQuery.put("$in", idList);
    query.put("_id", idQuery);
    // Add sort query

    DBCursor results = messageColl.find(query, keys);
    BasicDBObject orderBy = new BasicDBObject();
    orderBy.put(sortField, sortDirection);
    results = results.sort(orderBy);
    for (DBObject result : results.toArray())
        sortedIds.add((String) result.get("_id"));
    return sortedIds;
}

From source file:com.linuxbox.enkive.workspace.mongo.MongoSearchResultUtils.java

License:Open Source License

public List<String> sortSearchResults(Collection<String> searchResultIds, String sortField, int sortDirection) {
    ArrayList<String> sortedIds = new ArrayList<String>();
    // Only want to return the ids
    BasicDBObject keys = new BasicDBObject();
    keys.put("_id", 1);
    keys.put(sortField, 1);//from  w w w  . j  a v a2 s. c o  m

    BasicDBObject query = new BasicDBObject();
    // Build object with IDs
    BasicDBList idList = new BasicDBList();
    for (String Id : searchResultIds)
        idList.add(ObjectId.massageToObjectId(Id));

    BasicDBObject idQuery = new BasicDBObject();
    idQuery.put("$in", idList);
    query.put("_id", idQuery);
    // Add sort query

    DBCursor results = searchResultColl.find(query, keys);
    BasicDBObject orderBy = new BasicDBObject();
    orderBy.put(sortField, sortDirection);
    results = results.sort(orderBy);
    for (DBObject result : results.toArray())
        sortedIds.add(((ObjectId) result.get("_id")).toString());

    return sortedIds;
}