mongodb游标异常-Java
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用cursor.next()方法可以获得下一个元素并增加光标位置。
每次迭代都会调用 Cursor.next() 3 次,因此在第二次迭代时,光标中将不再有元素。
将元素保存在迭代的本地变量中:
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: