Example usage for com.mongodb DBCursor toArray

List of usage examples for com.mongodb DBCursor toArray

Introduction

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

Prototype

public List<DBObject> toArray() 

Source Link

Document

Converts this cursor to an array.

Usage

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

License:Open Source License

public List<String> sortSearchResults(Collection<String> searchResultIds, String sortField, int sortDirection,
        int pageNum, int pageSize) {
    ArrayList<String> sortedIds = new ArrayList<String>(pageSize);
    // Only want to return the ids
    BasicDBObject keys = new BasicDBObject();
    keys.put("_id", 1);
    keys.put(sortField, 1);/*ww  w . j a  va  2 s. co  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).skip((pageNum - 1) * pageSize).limit(pageSize);
    for (DBObject result : results.toArray())
        sortedIds.add((String) result.get("_id"));

    return sortedIds;
}

From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java

License:Apache License

@Override
public List<DBObject> findAll(String entityName, String projection, Pageable page) {
    if (page == null) {
        page = new PageRequest(0, Integer.MAX_VALUE);
    }/*from   w w w . jav a2s . c om*/

    DBCursor cursor = null;
    if (StringUtils.isEmpty(projection)) {
        cursor = getCollection(entityName).find(new BasicDBObject());
    } else {
        String[] properties = projection.split(",");
        BasicDBObjectBuilder projectionBuilder = BasicDBObjectBuilder.start();
        boolean idWanted = false;
        for (String property : properties) {
            property = property.trim();
            if (!StringUtils.isEmpty(property)) {
                if (property.equals(EntityUtils.ID)) {
                    idWanted = true;
                }

                projectionBuilder.add(property.trim(), true);
            }
        }

        if (idWanted == false) {
            projectionBuilder.append("_id", false);
        }

        cursor = getCollection(entityName).find(new BasicDBObject(), projectionBuilder.get())
                .sort(new BasicDBObject(projection, 1));
    }

    if (page.getSort() != null) {
        cursor = cursor.sort(createSort(page));
    } else if (projection != null) {
        cursor = cursor.sort(new BasicDBObject(projection, 1));
    }

    cursor = cursor.skip(page.getOffset()).limit(page.getPageSize());

    List<DBObject> result = cursor.toArray();
    return result;
}

From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java

License:Apache License

/**
 * {@inheritDoc}/*w w  w  .jav  a  2  s.  co  m*/
 * 
 */
@Override
public List<DBObject> findAllByQuery(DBObject query, String projection, Pageable page) {
    DBCursor cursor = null;
    if (StringUtils.isEmpty(projection)) {
        cursor = getCollection().find(query);
    } else {
        cursor = getCollection().find(query, new BasicDBObject(projection, true).append("_id", false));
    }

    if (page.getSort() != null) {
        cursor = cursor.sort(createSort(page));
    } else if (projection != null) {
        cursor = cursor.sort(new BasicDBObject(projection, 1));
    }

    cursor = cursor.skip(page.getOffset()).limit(page.getPageSize());

    List<DBObject> result = cursor.toArray();
    return result;
}

From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java

License:Apache License

@Override
public List<DBObject> findAllByQuery(String entityName, DBObject query) {
    if (query.containsField(EntityUtils.NAME)) {
        String name = (String) query.get(EntityUtils.NAME);
        query.put(EntityUtils.NAME,//  w w  w.  jav a 2  s .co m
                new BasicDBObject("$regex", "^" + name.toLowerCase() + "$").append("$options", "i"));
    }

    DBCursor cursor = getCollection(entityName).find(query);
    List<DBObject> result = cursor.toArray();
    return result;
}

From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java

License:Apache License

/**
 * @param entityName/*www  . j a  v  a2s .  co m*/
 * @param query
 * @return list of all documents by query
 */
@Override
public List<DBObject> findAllByQuery(String entityName, DBObject query, DBObject projection) {
    if (query.containsField(EntityUtils.NAME)) {
        String name = (String) query.get(EntityUtils.NAME);
        query.put(EntityUtils.NAME,
                new BasicDBObject("$regex", "^" + name.toLowerCase() + "$").append("$options", "i"));
    }

    final DBCursor cursor;
    if (projection != null && projection.keySet().size() > 0) {
        cursor = getCollection(entityName).find(query, projection);
    } else {
        cursor = getCollection(entityName).find(query);
    }

    List<DBObject> result = cursor.toArray();
    return result;
}

From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java

License:Apache License

@Override
public List<DBObject> findAllByQuery(String entityName, DBObject query, Pageable page) {

    if (query.containsField(EntityUtils.NAME)) {
        String name = (String) query.get(EntityUtils.NAME);
        query.put(EntityUtils.NAME,/*from  w ww.  j  a  va2s .  c  om*/
                new BasicDBObject("$regex", "^" + name.toLowerCase() + "$").append("$options", "i"));
    }

    DBCursor cursor = null;
    if (page.getSort() != null) {
        cursor = getCollection(entityName).find(query).sort(createSort(page)).skip(page.getOffset())
                .limit(page.getPageSize());
    } else {
        cursor = getCollection(entityName).find(query).skip(page.getOffset()).limit(page.getPageSize());
    }

    List<DBObject> result = cursor.toArray();
    return result;
}

From source file:com.mobileman.kuravis.core.services.treatment_review.impl.TreatmentReviewServiceImpl.java

License:Apache License

/** 
 * {@inheritDoc}/* w w w.java  2  s  . co  m*/
 * @see com.mobileman.kuravis.core.services.treatment_review.TreatmentReviewService#deleteAllTreatmentReviews(java.lang.String)
 */
@Override
public DBObject deleteAllTreatmentReviews(String userId) {
    if (StringUtils.isEmpty(userId)) {
        return ErrorUtils.error("Undefine user ID", ErrorCodes.INCORRECT_PARAMETER);
    }

    DBCursor cursor = getMongoTemplate().getCollection(ENTITY_NAME).find(
            new BasicDBObject("author._id", userId),
            new BasicDBObject("disease", 1).append(EntityUtils.TREATMENT, 1).append("author", 1));
    for (DBObject review : cursor.toArray()) {
        delete(review);
    }

    return ErrorUtils.success();
}

From source file:com.pavel.testtask.highway.MongoInitionalizer.java

@Override
public Set<Gate> getEnterPoints() {
    DBCollection table = db.getCollection("EnterPoint");
    DBCursor cur = table.find();
    List<DBObject> pointList = cur.toArray();

    Set<Gate> enterPointsSet = new HashSet<>();
    for (DBObject drv : pointList) {
        Gate d = new Gate((String) drv.get("name"), (float) ((int) drv.get("distance")));
        enterPointsSet.add(d);/*from  w  w w  . j a  va 2  s .  c o m*/
    }
    return enterPointsSet;
}

From source file:com.pavel.testtask.highway.MongoInitionalizer.java

@Override
public Set<Driver> getRegistredDrivers() {
    DBCollection table = db.getCollection("Driver");
    DBCursor cur = table.find();
    List<DBObject> driverList = cur.toArray();

    Set<Driver> driversSet = new HashSet<>();

    for (DBObject drv : driverList) {
        driversSet.add(new Driver((int) drv.get("id"), (String) drv.get("email")));
    }//from w  w  w  .j  a v  a  2s . c  o m
    return driversSet;
}

From source file:com.redhat.lightblue.crud.mongo.BasicDocFinder.java

License:Open Source License

@Override
public long find(CRUDOperationContext ctx, DBCollection coll, DBObject mongoQuery, DBObject mongoSort,
        Long from, Long to) {
    LOGGER.debug("Submitting query");
    DBCursor cursor = new FindCommand(null, coll, mongoQuery, null).execute();
    LOGGER.debug("Query evaluated");
    if (mongoSort != null) {
        cursor = cursor.sort(mongoSort);
        LOGGER.debug("Result set sorted");
    }/*www.  ja v  a2 s  .c o  m*/
    long ret = cursor.size();
    LOGGER.debug("Applying limits: {} - {}", from, to);
    if (from != null) {
        cursor.skip(from.intValue());
    }
    if (to != null) {
        cursor.limit(to.intValue() - (from == null ? 0 : from.intValue()) + 1);
    }
    LOGGER.debug("Retrieving results");
    List<DBObject> mongoResults = cursor.toArray();
    LOGGER.debug("Retrieved {} results", mongoResults.size());
    List<JsonDoc> jsonDocs = translator.toJson(mongoResults);
    ctx.addDocuments(jsonDocs);
    for (DocCtx doc : ctx.getDocuments()) {
        doc.setOperationPerformed(Operation.FIND);
    }
    LOGGER.debug("Translated DBObjects to json");
    return ret;
}