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