Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MongoDB和Java中从DbCursor创建列表_Java_Mongodb - Fatal编程技术网

在MongoDB和Java中从DbCursor创建列表

在MongoDB和Java中从DbCursor创建列表,java,mongodb,Java,Mongodb,我试图使用游标来迭代文档,我想将它们存储在一个列表中,然后返回一个dbobject类型的列表 以下是我正在尝试的: public List<DBObject> getResultsInDescendingOrderByDate(int limit) { List<DBObject> myList = null; DBCursor myCursor=myCollection.find().sort(new BasicDBObject("da

我试图使用游标来迭代文档,我想将它们存储在一个列表中,然后返回一个dbobject类型的列表

以下是我正在尝试的:

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;
    }
public List getResultsInDescendingOrderByDate(整数限制){
列表myList=null;
DBCursor myCursor=myCollection.find().sort(新的BasicDBObject(“日期”,-1)).limit(10);
试一试{
while(myCursor.hasNext()){
System.out.print(myCursor.next());
添加(新的BasicDBObject(“\u id”,(字符串)myCursor.curr().get(“\u id”))
.append(“title”,(字符串)myCursor.curr().get(“title”))
.append(“author”,(字符串)myCursor.curr().get(“author”))
.append(“permalink”,(字符串)myCursor.curr().get(“permalink”))
.append(“body”,(String)myCursor.curr().get(“body”))
.append(“comment”,新的BasicDBObject(“comments”,字符串)myCursor.curr().get(“comments”))
.append(“tags”,新的BasicDBObject(“tags”,字符串)myCursor.curr().get(“tags”))
.append(“date”,(date)myCursor.curr().get(“date”);
myCursor.next();
}
}
最后{
myCursor.close();
}
返回myList;
}
我不知道如何将数据类型从游标转换为基本类型。我试着寻找,但没有任何线索

请帮忙


感谢您所做的一切,无需阅读各个字段。你必须初始化你的列表。另外,调用next()两次,在print语句中调用一次。您可以只使用next()的返回值,而不调用curr()。哦,有人正确地建议你应该通过“限制”论点,而不是使用10,除非这是有意的:

public List<DBObject> getResultsInDescendingOrderByDate(int limit) {
    List<DBObject> myList = new ArrayList<DBObject>();
    DBCursor myCursor=myCollection.find().sort(new BasicDBObject("date",-1)).limit(limit);
    try {
        while(myCursor.hasNext()) {
            myList.add(myCursor.next());
        }
    }
    finally {
        myCursor.close();
    }
    return myList;
}
public List getResultsInDescendingOrderByDate(整数限制){
List myList=new ArrayList();
DBCursor myCursor=myCollection.find().sort(新的BasicDBObject(“日期”,-1)).limit(限制);
试一试{
while(myCursor.hasNext()){
添加(myCursor.next());
}
}
最后{
myCursor.close();
}
返回myList;
}

@sdanzig解决方案将起作用,但。。。 如果希望键入较少的代码,可以执行以下操作:

public List<DBObject> getResultsInDescendingOrderByDate(int limit) {

        List<DBObject> myList = null;
        DBCursor myCursor=myCollection.find().sort(new BasicDBObject("date",-1)).limit(10);
        myList = myCursor.toArray();

        return myList;
    }
public List getResultsInDescendingOrderByDate(整数限制){
列表myList=null;
DBCursor myCursor=myCollection.find().sort(新的BasicDBObject(“日期”,-1)).limit(10);
myList=myCursor.toArray();
返回myList;
}

DBCursor方法返回一个列表,在我使用的示例中:

List employees=(List)collection.find().into(
新的ArrayList());

hi-sdanzig,非常感谢您的帮助。我让它工作了。@sdanzig您能解释一下为什么要关闭光标吗?不关闭光标有任何缺点吗?@PratikPatel光标会消耗资源,因此关闭它们可以保持整洁。游标用尽时(完全迭代)会关闭,但如果只是部分迭代,显式关闭游标有助于避免等待10分钟超时释放资源。@sdanzig是您在Mongo Doc中记录的内容吗?“我找不到它,你能给我URL吗,thx?”PratikPatel根据我在这里读到的内容:。。。询问Neil Lunn:)警告:在DBCursor上调用toArray或length将不可避免地将其转换为数组。这意味着,如果光标在一千万个结果上迭代(它是从数据库中懒洋洋地获取的),那么内存中将突然出现一个一千万个元素的数组。在转换为数组之前,请确保使用skip()和limit()得到的结果数量合理。例如,要获取游标第1000-1100个元素的数组,请使用List obj=collection.find(query).skip(1000).limit(100).toArray();当然,toArray方法可能是一个有大量结果的问题,但这正是问题所在——如何将光标转到列表。我并不反对这个答案,我认为它是正确的,我只是在光标有大量结果的情况下添加了一个警告。
List<Document> employees = (List<Document>) collection.find().into(
                new ArrayList<Document>());