ApJo
ApJo

Reputation: 185

Create a list from DbCursor in MongoDB and Java

I am trying to use a cursor to iterate over documents, i want to store them in a list,and later on return a list of type DBOject.

Here is what I am trying:

public List<DBObject> getResultsInDescendingOrderByDate(int limit) {

        List<DBObject> myList = null;
        DBCursor myCursor=myCollection.find().sort(new BasicDBObject("date",-1)).limit(10);
        try {
        while(myCursor.hasNext()) {

                System.out.print(myCursor.next());
               myList.add(new BasicDBObject("_id",(String) myCursor.curr().get("_id"))
                        .append("title",(String) myCursor.curr().get("title"))
                        .append("author",(String) myCursor.curr().get("author"))
                        .append("permalink",(String) myCursor.curr().get("permalink"))
                        .append("body",(String) myCursor.curr().get("body"))
                        .append("comment",new BasicDBObject("comments",(String) myCursor.curr().get("comments")))
                                .append("tags",new BasicDBObject("tags",(String) myCursor.curr().get("tags"))
                                .append("date",(Date) myCursor.curr().get("date"))));
                myCursor.next();
            }
        }

        finally {
            myCursor.close();
        }


        return myList;
    }

I don't know how to convert the data type into a primitive one form the cursor. I tried searching,but no clues.

Please help.

Thanks

Upvotes: 9

Views: 22020

Answers (3)

hestellezg
hestellezg

Reputation: 3711

In my case I'm working with Documents:

List<Document> employees = (List<Document>) collection.find().into(
                new ArrayList<Document>());

Upvotes: 1

Dmitri
Dmitri

Reputation: 36280

@sdanzig solution will work but... If you like to type less code you can do this:

public List<DBObject> getResultsInDescendingOrderByDate(int limit) {

        List<DBObject> myList = null;
        DBCursor myCursor=myCollection.find().sort(new BasicDBObject("date",-1)).limit(10);
        myList = myCursor.toArray();

        return myList;
    }

the DBCursor.toArray() method of DBCursor returns a List

Upvotes: 16

sdanzig
sdanzig

Reputation: 4500

For what you're trying to do, no need to read the individual fields. You do have to initialize your list. Plus, you were calling next() twice, once in the print statement. You can just use the return value of next() rather than calling curr(). Oh, and someone rightly suggested you should pass in the "limit" argument rather than use 10, unless that was intentional:

public List<DBObject> getResultsInDescendingOrderByDate(int limit) {
    List<DBObject> myList = new ArrayList<DBObject>();
    DBCursor myCursor=myCollection.find().sort(new BasicDBObject("date",-1)).limit(limit);
    try {
        while(myCursor.hasNext()) {
            myList.add(myCursor.next());
        }
    }
    finally {
        myCursor.close();
    }
    return myList;
}

Upvotes: 6

Related Questions