在 MongoDB 和 Java 中从 DbCursor 创建一个列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19580501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:30:39  来源:igfitidea点击:

Create a list from DbCursor in MongoDB and Java

javamongodb

提问by ApJo

I am trying to use a cursor to iterate over documents, i want to store them in a list,and later on return a list of type DBOject.

我正在尝试使用游标来遍历文档,我想将它们存储在一个列表中,然后返回一个 DBOject 类型的列表。

Here is what I am trying:

这是我正在尝试的:

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

I don't know how to convert the data type into a primitive one form the cursor. I tried searching,but no clues.

我不知道如何将数据类型转换为游标的原始数据类型。我尝试搜索,但没有任何线索。

Please help.

请帮忙。

Thanks

谢谢

采纳答案by Dmitri

@sdanzig solution will work but... If you like to type less code you can do this:

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

the DBCursor.toArray()method of DBCursor returns a List

所述DBCursor.toArray()DBCursor的方法返回一个列表

回答by sdanzig

For what you're trying to do, no need to read the individual fields. You do have to initialize your list. Plus, you were calling next() twice, once in the print statement. You can just use the return value of next() rather than calling curr(). Oh, and someone rightly suggested you should pass in the "limit" argument rather than use 10, unless that was intentional:

对于您要执行的操作,无需阅读各个字段。您必须初始化您的列表。另外,您调用 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;
}

回答by estellezg

In my case I'm working with Documents:

就我而言,我正在使用Documents

List<Document> employees = (List<Document>) collection.find().into(
                new ArrayList<Document>());