while I'm trying to read data from Mongodb I'm getting this Exception,
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)
MyCode
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")));
}
By using the above code I'm reading first record from set of records in Mongodb, but the line cursor.hasNext() throwing IllegalStateException. Please suggest me how to resolve this issue..
Thats why you call mongo.close(); befor calling cursor.hasNext()).
Remove the mongo.close(); from firstRecord and add it after your loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With