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:homework.week3.course.BlogPostDAO.java

License:Apache License

public List<DBObject> findByDateDescending(int limit) {

    List<DBObject> posts = null;
    // XXX HW 3.2, Work Here
    // Return a list of DBObjects, each one a post from the posts collection
    QueryBuilder sortQuery = QueryBuilder.start("date").is(-1);
    DBCursor cursor = postsCollection.find().sort(sortQuery.get()).limit(limit);
    posts = cursor.toArray();
    return posts;
}

From source file:logica.MovieSessionBean.java

public List<Movie> getMovies() {
    List<Movie> movies = new ArrayList();
    DBCursor cur = movieColl.find();
    System.out.println("getMovies: Found" + cur.size() + "movie(s)");
    for (DBObject dbo : cur.toArray()) {
        movies.add(Movie.fromDBObject(dbo));
    }//from   ww  w  .  jav  a  2 s .  c o m
    return movies;
}

From source file:logica.MovieSessionBean.java

public List<Movie> getMovie(String nombre) {
    BasicDBObject query = new BasicDBObject();
    List<Movie> movies = new ArrayList();
    query.put("name", nombre);
    DBCursor db = movieColl.find(query);
    for (DBObject dbo : db.toArray()) {
        movies.add(Movie.fromDBObject(dbo));
    }/*from w  w  w . j a v a2s.  com*/
    return movies;
}

From source file:logica.MovieSessionBean.java

public List<Movie> getMovieYear(int year) {
    BasicDBObject query = new BasicDBObject();
    List<Movie> movies = new ArrayList();
    query.put("year", year);
    DBCursor db = movieColl.find(query);
    for (DBObject dbo : db.toArray()) {
        movies.add(Movie.fromDBObject(dbo));
    }//  ww w .  ja  v a  2 s .  c  o  m
    return movies;
}

From source file:logica.MovieSessionBean.java

public List<Movie> getMovieLenguage(String lenguage) {
    BasicDBObject query = new BasicDBObject();
    List<Movie> movies = new ArrayList();
    query.put("lenguage", lenguage);
    DBCursor db = movieColl.find(query);
    for (DBObject dbo : db.toArray()) {
        movies.add(Movie.fromDBObject(dbo));
    }//from  www .j a v  a 2s.c  o  m
    return movies;
}

From source file:me.schiz.jmeter.protocol.mongodb.sampler.MongoFindSampler.java

License:Apache License

@Override
public SampleResult sample(Entry e) {
    SampleResult res = new SampleResult();
    //String data = getScript();

    BasicDBObject document = (BasicDBObject) JSON.parse(getDocument());

    res.setSampleLabel(getTitle());/*from  w  w  w  .j  av a  2s  .  co m*/
    res.setResponseCodeOK();
    res.setSuccessful(true);
    res.setResponseMessageOK();
    res.setSamplerData("db." + getCollection() + ".find(" + document + ")");
    res.setDataType(SampleResult.TEXT);
    res.setContentType("text/plain"); // $NON-NLS-1$
    res.sampleStart();

    try {
        MongoDB mongoDB = MongoSourceElement.getMongoDB(getSource());
        DB db = mongoDB.getDB(getDatabase(), getUsername(), getPassword());
        res.latencyEnd();
        DBCursor cursor = db.getCollection(getCollection()).find(document);
        String response = new String();
        List<DBObject> resultList = cursor.toArray();
        if (resultList != null || resultList.isEmpty()) {
            for (DBObject o : resultList) {
                response += o.toString() + "\n";
            }
            res.setResponseData(response.getBytes());
        } else {
            res.setResponseCode("404");
            res.setSuccessful(false);
        }
    } catch (Exception ex) {
        res.setResponseCode("500"); // $NON-NLS-1$
        res.setSuccessful(false);
        res.setResponseMessage(ex.toString());
        res.setResponseData(ex.getMessage().getBytes());
    } finally {
        res.sampleEnd();
    }
    return res;
}

From source file:models.Bookmark.java

License:Open Source License

public static List<NodeContent> getUpdatesForBookmark(String nodeId, ObjectId uid) {
    // 1. get bookmark
    List<NodeContent> newNodes = null;
    Bookmark b = getByUserAndDest(uid, nodeId);
    Long lastVisit = b.lastVisit;
    updateVisit(uid, nodeId);/*from w  w w. j a  v a2  s.c o m*/
    // 2. load notifications
    try {
        BasicDBObject query = new BasicDBObject().append("ids", new ObjectId(nodeId)).append("date",
                new BasicDBObject("$gt", lastVisit));
        // Logger.info("getUpdatesForBookmark::"  + query.toString());
        // BasicDBObject sort = new BasicDBObject().append("date", -1);
        // vlastne chceme natural sort a iba idcka nodes ktore mame zobrazit
        DBCursor iobj = MongoDB.getDB().getCollection(MongoDB.CActivity).find(query).sort(sort);
        if (iobj != null) {
            // Logger.info("getUpdatesForBookmark found");
            List<Activity> lll = Lists.transform(iobj.toArray(), MongoDB.getSelf().toActivity());
            // 3. load nodes we want to show
            List<ObjectId> nodeIds = new LinkedList<ObjectId>();
            for (Activity ac : lll)
                nodeIds.add(ac.getOid());
            newNodes = NodeContent.load(nodeIds);
        }
    } catch (Exception ex) {
        Logger.info("getUpdatesForBookmark");
        ex.printStackTrace();
        Logger.info(ex.toString());
    }
    return newNodes;
}

From source file:models.Bookmark.java

License:Open Source License

public static List<Bookmark> getByDest(ObjectId dest) {
    List<Bookmark> b = null;
    try {//from  w  ww .  j  a va 2s.com
        DBCursor iobj = MongoDB.getDB().getCollection(MongoDB.CBookmark)
                .find(new BasicDBObject().append(DEST, dest));
        // Logger.info("getByDest::" + iobj);
        if (iobj != null)
            b = Lists.transform(iobj.toArray(), MongoDB.getSelf().toBookmark());
        else
            b = new LinkedList<Bookmark>();
    } catch (Exception ex) {
        Logger.info("getByDest::" + dest);
        ex.printStackTrace();
        Logger.info(ex.toString());
    }
    return b;
}

From source file:models.Feed.java

License:Open Source License

static List<Feed> load(List<ObjectId> feedIds) {
    List<Feed> feeds = null;
    try {//from www . j a  v  a 2 s.  com
        DBObject query = new BasicDBObject("_id",
                new BasicDBObject().append("$in", feedIds.toArray(new ObjectId[feedIds.size()])));
        DBCursor iobj = MongoDB.getDB().getCollection(MongoDB.CFeed).find(query);
        if (iobj != null)
            feeds = Lists.transform(iobj.toArray(), MongoDB.getSelf().toFeed()); //? .toFeedWithContent()
    } catch (Exception ex) {
        Logger.info("load feeds::");
        ex.printStackTrace();
        Logger.info(ex.toString());
    }
    return feeds;
}

From source file:models.Message.java

License:Open Source License

public static List<Message> getLastMessages(ObjectId threadId, boolean doUpdate, ObjectId forUser,
        Integer start, Integer count) {
    BasicDBObject query = new BasicDBObject().append("thread", threadId);
    BasicDBObject sort = new BasicDBObject().append("sent", -1);
    if (start == null)
        start = 0;//from   ww  w . j a va 2  s  .  co  m
    if (count == null)
        count = 30;
    DBCursor iobj = MongoDB.getDB().getCollection(MongoDB.CMessage).find(query).sort(sort).skip(start)
            .limit(count);
    if (doUpdate)
        MessageThread.setAsRead(threadId, forUser);
    // Hilarity ensues :)
    return Lists.transform(iobj.toArray(), MongoDB.getSelf().toMessage());
}