我正在使用DBCursor
在MongoDB数据库中打印文档。当我使用while循环时,它工作正常。
DBCursor cursor = people.find(new BasicDBObject("isPerson",true));
while(cursor.hasNext()){
System.out.println(cursor.next());
}
cursor.close();
使用while循环,打印文档。当我使用for循环时,我收到错误。
cursor = people.find(new BasicDBObject("isPerson",true));
for(int a=0;a<cursor.length()-1;a++){
System.out.println(cursor.next());
}
cursor.close();
这是错误:
java.lang.IllegalArgumentException: Can't switch cursor access methods
at com.mongodb.DBCursor.checkIteratorOrArray(DBCursor.java:841)
at com.mongodb.DBCursor.next(DBCursor.java:168)
at mongotest.MongoTest.main(MongoTest.java:39)
但即使我使用以下代码解决错误,也不会打印任何内容。
cursor = people.find(new BasicDBObject("isPerson",true));
int b = cursor.length()-1;
cursor = people.find(new BasicDBObject("isPerson",true));
for(int a=0;a<b;a++){
System.out.println(cursor.next());
}
cursor.close();
答案 0 :(得分:1)
"click input[type='checkbox']": function(){
//changing state task completed
this.completed = !this.completed;
//getting active user
var customer = Session.get('active_user').player_number
var data = {'task_id': this.id,
'completed': this.completed };
//Changing state of task completed
Meteor.call('setTaskCompleted', data, function(error, result){
if(error){
console.log(error);
}else{
setCustomerTodo(customer);
}
});
}
明确提到if!hasNext()抛出异常
另外
如果将cursor.length()保留在for循环中并通过调用cursor.next()继续迭代,则游标长度会保持变化并抛出异常。
查看您的替代方法,您已使用游标长度初始化变量i,并且在执行FOR循环期间i的值不会更改。