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;
}


我在发布和obj时遇到错误,错误是:

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


谁能帮我这个?

最佳答案

您正在尝试将DBObject分配给BasicDBObject。基本上将泛型类型分配给特定类型。这是不允许的。将您的列表更改为List<DBObject>obj更改为BasicDBObject

10-08 07:58