duke
duke

Reputation: 103

Resolving MongoDB error

public List<DBObject> findByDateDescending(int limit) {

    List<BasicDBObject> posts = null;
    // XXX HW 3.2,  Work Here
    // Return a list of DBObjects, each one a post from the posts collection
    BasicDBObject query = new BasicDBObject();

    BasicDBObject sortPredicate = new BasicDBObject();
    sortPredicate.put("date", -1);

    DBCursor cur = postsCollection.find(query).sort(sortPredicate);

    int i = 0;
    while( cur.hasNext() && i < limit ) {
        if ( posts == null)
            posts = new ArrayList<BasicDBObject>();

        DBObject obj = cur.next();
        posts.add(obj);
        System.out.println("findByDateDescending(" + limit + " blog entry " + obj);
        i++;
    }

    return posts;
}

I am getting an error at the posts and obj and the error is:

add(com.mongodb.BasicDBObject)List cannot be applied to com.mongodb.DBObject.

Can anyone help me with this?

Upvotes: 0

Views: 114

Answers (1)

coder
coder

Reputation: 1941

You're trying to assign DBObject to BasicDBObject. Basically assigning a generic type to specific type. This is not allowed. Change your list to be List<DBObject> or obj to be BasicDBObject

Upvotes: 1

Related Questions