gpt4 book ai didi

java.lang.IllegalStateException : open 错误

转载 作者:可可西里 更新时间:2023-11-01 09:30:14 26 4
gpt4 key购买 nike

当我尝试从 Mongodb 读取数据时,我遇到了这个异常,

java.lang.IllegalStateException: open
at org.bson.util.Assertions.isTrue(Assertions.java:36)
at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:369)
at com.mongodb.Mongo.isMongosConnection(Mongo.java:645)
at com.mongodb.DBCursor._check(DBCursor.java:454)
at com.mongodb.DBCursor._hasNext(DBCursor.java:546)
at com.mongodb.DBCursor.hasNext(DBCursor.java:571)
at com.calsoftlabs.mongo.client.impl.ReadingData.main(ReadingData.java:93)

我的代码

    public static DBCursor firstRecord(String ip) throws Exception {

DBObject query = new BasicDBObject("client_ip", ip);

DBCollection collection = getConnection();

DBObject obj = new BasicDBObject("_id", 1);

DBCursor cursor = collection.find(query).sort(obj).limit(1);

mongo.close();

return cursor;

}
DBCursor cursor = ReadingData.firstRecord(ip);

while (cursor.hasNext()) {

cursor.next();

myList.add(new BasicDBObject("client_ip", (String) cursor.curr().get(
"client_ip")).append("timestamp",
(String) cursor.curr().get("timestamp")).append("total_traffic",
(String) cursor.curr().get("total_traffic")));

}

通过使用上面的代码,我正在从 Mongodb 的记录集中读取第一条记录,但是 cursor.hasNext() 行抛出 IllegalStateException。请建议我如何解决此问题..

最佳答案

这就是为什么在调用 cursor.hasNext()) 之前调用 mongo.close(); 的原因。从 first Record 中删除 mongo.close(); 并将其添加到循环之后

关于java.lang.IllegalStateException : open 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28447801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com