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:calliope.db.MongoConnection.java

License:Open Source License

/**
 * List all the documents in a Mongo collection
 * @param collName the name of the collection
 * @return a String array of document keys
 * @throws AeseException //from  www  .j a  v  a  2s . co m
 */
@Override
public String[] listCollection(String collName) throws AeseException {
    if (!collName.equals(Database.CORPIX)) {
        try {
            connect();
        } catch (Exception e) {
            throw new AeseException(e);
        }
        DBCollection coll = getCollectionFromName(collName);
        BasicDBObject keys = new BasicDBObject();
        keys.put(JSONKeys.DOCID, 1);
        DBCursor cursor = coll.find(new BasicDBObject(), keys);
        System.out.println("Found " + cursor.count() + " documents");
        cursor.count();
        if (cursor.length() > 0) {
            String[] docs = new String[cursor.length()];
            Iterator<DBObject> iter = cursor.iterator();
            int i = 0;
            while (iter.hasNext())
                docs[i++] = (String) iter.next().get(JSONKeys.DOCID);
            return docs;
        } else {
            return new String[0];
        }
    } else {
        GridFS gfs = new GridFS(db, collName);
        DBCursor curs = gfs.getFileList();
        int i = 0;
        List<DBObject> list = curs.toArray();
        HashSet<String> set = new HashSet<String>();
        Iterator<DBObject> iter = list.iterator();
        while (iter.hasNext()) {
            String name = (String) iter.next().get("filename");
            set.add(name);
        }
        String[] docs = new String[set.size()];
        set.toArray(docs);
        return docs;
    }
}

From source file:ch.windmobile.server.social.mongodb.ChatServiceImpl.java

License:Open Source License

@Override
public Messages findMessages(String chatRoomId, int maxCount) {
    if (maxCount <= 0) {
        throw new IllegalArgumentException("maxCount arg[1] must be greater than 0");
    }/*w  ww. jav  a  2  s  . c  o  m*/
    final String collectionName = computeCollectionName(chatRoomId);

    final DBObject fields = BasicDBObjectBuilder.start("_id", 1).add(MongoDBConstants.CHAT_PROP_USER, 1)
            .add(MongoDBConstants.CHAT_PROP_TEXT, 1).add(MongoDBConstants.CHAT_PROP_TIME, 1)
            .add(MongoDBConstants.CHAT_PROP_EMAIL_HASH, 1).get();
    DBCursor result = db.getCollection(collectionName).find(null, fields)
            .sort(new BasicDBObject("$natural", -1)).limit(maxCount);
    List<DBObject> all = result.toArray();

    Messages messages = new Messages();
    for (DBObject dbObject : all) {
        Message message = new Message();
        message.setId(dbObject.get("_id").toString());
        DateTime dateTime = ISODateTimeFormat.dateTime().parseDateTime((String) dbObject.get("time"));
        message.setDate(dateTime);
        message.setPseudo((String) dbObject.get(MongoDBConstants.CHAT_PROP_USER));
        message.setText((String) dbObject.get(MongoDBConstants.CHAT_PROP_TEXT));
        message.setEmailHash((String) dbObject.get(MongoDBConstants.CHAT_PROP_EMAIL_HASH));

        messages.getMessages().add(message);
    }
    return messages;
}

From source file:com.affairs.dao.zaffar.CurrentAffairsDAO.java

License:Apache License

public List<DBObject> findAllSayDoProjects() {

    //List<DBObject> sayDoList = new ArrayList<DBObject>();

    DBObject whereCondition = new BasicDBObject().append("isCurrent", true).append("delInd", false);
    DBObject sortCondition = new BasicDBObject().append("tower", 1).append("platform", 1).append("program", 1)
            .append("Project", 1);

    DBCursor sayDoListCursor = this.saydoCollection.find(whereCondition).sort(sortCondition)
            .limit(SAYDO_UI_DISPLAY_LIMIT);

    /*DBObject sayDoCursor = new BasicDBObject();
            /*from www  .j  a  va2  s.  c  o m*/
    try {
    while (sayDoListCursor.hasNext()) {
        sayDoCursor = sayDoListCursor.next();
        sayDoList.add(sayDoCursor);
    }
    } catch (Exception exp) {
    System.out.println(exp.getMessage());
    } finally {
    sayDoListCursor.close();
    }*/
    return sayDoListCursor.toArray();
}

From source file:com.andiandy.m101j.week3.hw2_3.BlogPostDAO.java

License:Apache License

public List<DBObject> findByDateDescending(int limit) {

    DBCursor cursor = postsCollection.find().sort(new BasicDBObject("date", -1)).limit(limit);
    List<DBObject> posts = cursor.toArray();

    // XXX HW 3.2,  Work Here
    // Return a list of DBObjects, each one a post from the posts collection

    return posts;
}

From source file:com.andiandy.m101j.week4.course.BlogPostDAO.java

License:Apache License

public List<DBObject> findByDateDescending(int limit) {
    List<DBObject> posts;/*from w  w  w .  j a  va 2  s .co  m*/
    DBCursor cursor = postsCollection.find().sort(new BasicDBObject().append("date", -1)).limit(limit);
    try {
        posts = cursor.toArray();
    } finally {
        cursor.close();
    }
    return posts;
}

From source file:com.andiandy.m101j.week4.course.BlogPostDAO.java

License:Apache License

public List<DBObject> findByTagDateDescending(final String tag) {
    List<DBObject> posts;/*  w  w  w.  j av  a2s . com*/
    BasicDBObject query = new BasicDBObject("tags", tag);
    System.out.println("/tag query: " + query.toString());
    DBCursor cursor = postsCollection.find(query).sort(new BasicDBObject().append("date", -1)).limit(10);
    try {
        posts = cursor.toArray();
    } finally {
        cursor.close();
    }
    return posts;
}

From source file:com.bbc.remarc.ws.ThemeServiceImpl.java

License:Apache License

@GET
@Produces("application/json")
public Response find() {

    List<DBObject> results = new ArrayList<DBObject>();

    DB db = MongoClient.getDB();//from  w w  w .j  ava2s .  com
    db.requestStart();

    DBCollection theme = db.getCollection(COLLECTION_NAME);
    DBCursor cursor = theme.find().limit(NUM_THEMES);

    if (cursor.length() != NUM_THEMES) {
        throw new WebApplicationException(Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR)
                .entity("unexpected number of themes returned").build());
    }

    results = cursor.toArray();

    db.requestDone();

    return Response.ok(String.format("%s", results)).build();
}

From source file:com.ebay.cloud.cms.dal.persistence.MongoExecutor.java

License:Apache License

public static List<DBObject> find(PersistenceContext context, MetaClass metadata, DBObject queryObject,
        DBObject fieldObject, SearchOption option) {
    long start = System.currentTimeMillis();
    String msg = "success";
    DBCollection dbCollection = context.getDBCollection(metadata);
    DBCursor findCursor = null;
    Integer size = 0;//from  w ww  .j ava2s .c  om
    try {
        findCursor = dbCollection.find(queryObject, fieldObject);
        // set option
        if (option.hasLimit()) {
            findCursor.limit(option.getLimit());
        }
        if (option.hasSkip()) {
            findCursor.skip(option.getSkip());
        }
        if (option.hasSort()) {
            findCursor.sort(option.getSort());
        }
        // populate search result
        List<DBObject> result = findCursor.toArray();
        size = result.size();
        return result;
    } catch (Throwable t) {
        msg = t.getMessage();
        handleMongoException(t);
    } finally {
        if (findCursor != null) {
            findCursor.close();
        }
        logMongoAction(context, "find", start, dbCollection, queryObject, fieldObject, option, size, msg);
    }
    return Collections.emptyList();
}

From source file:com.ebay.oss.bark.repo.DqMetricsRepoImpl.java

License:Apache License

@Override
public DqMetricsValue getLatestByAssetId(String assetId) {
    DBCursor temp = dbCollection.find(new BasicDBObject("assetId", assetId));
    List<DBObject> all = temp.toArray();
    long latest = 0L;
    DBObject latestObject = null;//from ww  w. j av a2 s.com
    for (DBObject o : all) {
        if (Long.parseLong(o.get("timestamp").toString()) - latest > 0) {
            latest = Long.parseLong(o.get("timestamp").toString());
            latestObject = o;
        }
    }

    if (latestObject == null) {
        return null;
    }
    return new DqMetricsValue(latestObject.get("metricName").toString(),
            Long.parseLong(latestObject.get("timestamp").toString()),
            Float.parseFloat(latestObject.get("value").toString()));
}

From source file:com.ebay.oss.bark.repo.DqMetricsRepoImpl.java

License:Apache License

@Override
public List<DqMetricsValue> getByMetricsName(String name) {
    DBCursor temp = dbCollection.find(new BasicDBObject("metricName", name));
    List<DBObject> all = temp.toArray();
    List<DqMetricsValue> result = new ArrayList<DqMetricsValue>();
    for (DBObject o : all) {
        result.add(toEntity(o));/*from www.  ja v  a2 s.co  m*/
    }
    return result;
}