mongodb游标异常-Java

发布于 2024-12-12 07:25:21 字数 1451 浏览 1 评论 0原文

我正在使用 mongodb 来存储用户信息。 我想创建一个方法来从数据库获取信息,创建 Player 对象并将它们插入到 Player 数组中。

这是以下方法

public ArrayList<Player> getArrayOfPlayers(){
    ArrayList<Player> savePlayers = new ArrayList<Player>();
    DB db = connectToMongo();
    DBCollection coll = db.getCollection("players");
    DBCursor cursor = coll.find();

        while(cursor.hasNext()) {
            String tempName = (String)cursor.next().get("name");
            String tempSession = (String)cursor.next().get("session");
            String tempStringScore = (String)cursor.next().get("score");

            int tempScore = Integer.parseInt(tempStringScore);

            Player player = new Player(tempName,tempSession,tempScore);
            savePlayers.add(player);
        }


    return savePlayers;
}

,我在数据库中存储了 4 个用户,当我尝试首先调用该方法然后打印名称时,例如我遇到了异常。 我在方法的 while 之外使用了 try-catch 并捕获了异常,但随后它仅打印了第一个用户的名称。看起来它在第二次迭代中抛出了异常。

这是我收到的除例外情况之外的消息。

java.lang.RuntimeException: no more
com.mongodb.DBApiLayer$Result.next(DBApiLayer.java:394)
com.mongodb.DBApiLayer$Result.next(DBApiLayer.java:360)
com.mongodb.DBCursor._next(DBCursor.java:445)
com.mongodb.DBCursor.next(DBCursor.java:525)
machine.DAOMongodb.getArrayOfPlayers(DAOMongodb.java:74)
machine.testDB.doGet(testDB.java:43)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I am using mongodb to store information of users.
I wanted to create a method that get the information from the db, creates Player objects and inserts them in an array of Players.

this is the following method

public ArrayList<Player> getArrayOfPlayers(){
    ArrayList<Player> savePlayers = new ArrayList<Player>();
    DB db = connectToMongo();
    DBCollection coll = db.getCollection("players");
    DBCursor cursor = coll.find();

        while(cursor.hasNext()) {
            String tempName = (String)cursor.next().get("name");
            String tempSession = (String)cursor.next().get("session");
            String tempStringScore = (String)cursor.next().get("score");

            int tempScore = Integer.parseInt(tempStringScore);

            Player player = new Player(tempName,tempSession,tempScore);
            savePlayers.add(player);
        }


    return savePlayers;
}

I have 4 users stored in the db and when I 'm trying to first call the method and then print the names for example I'm getting an exception.
I used a try-catch outside the method's while and I caught the exception, but then it printed the name of the first user only. It seems that it throws the exception in the second iteration.

Here is the message with the exception that I am receiving.

java.lang.RuntimeException: no more
com.mongodb.DBApiLayer$Result.next(DBApiLayer.java:394)
com.mongodb.DBApiLayer$Result.next(DBApiLayer.java:360)
com.mongodb.DBCursor._next(DBCursor.java:445)
com.mongodb.DBCursor.next(DBCursor.java:525)
machine.DAOMongodb.getArrayOfPlayers(DAOMongodb.java:74)
machine.testDB.doGet(testDB.java:43)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

究竟谁懂我的在乎 2024-12-19 07:25:21

调用cursor.next()方法可以获得下一个元素并增加光标位置。
每次迭代都会调用 Cursor.next() 3 次,因此在第二次迭代时,光标中将不再有元素。
将元素保存在迭代的本地变量中:

while(cursor.hasNext()) {
        DBObject tobj = cursor.next();  
        String tempName = (String)tobj.get("name");
        String tempSession = (String)tobj.get("session");
        String tempStringScore = (String)tobj.get("score");

        int tempScore = Integer.parseInt(tempStringScore);

        Player player = new Player(tempName,tempSession,tempScore);
        savePlayers.add(player);
    }

Calling the cursor.next() method you get the next element and increment cursor position.
You call cursor.next() 3 times for each iteration so at second iteration you have "no more" element in the cursor.
Save the element in a variable local to the iteration:

while(cursor.hasNext()) {
        DBObject tobj = cursor.next();  
        String tempName = (String)tobj.get("name");
        String tempSession = (String)tobj.get("session");
        String tempStringScore = (String)tobj.get("score");

        int tempScore = Integer.parseInt(tempStringScore);

        Player player = new Player(tempName,tempSession,tempScore);
        savePlayers.add(player);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文