简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM