解决MongoDB错误

时间:2014-07-26 10:15:21

标签: java mongodb

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.

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:1)

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