how to resolve com.mongodb.FindIterableImpl class cast exception

1.5k views Asked by At

I am trying to retrieve mongodb document from a collection using the below code. But I am getting class cast exception.

DBCursor cursor = (DBCursor) collection.find(new BasicDBObject("_id", "theId")); 

console Error: Exception in thread "main" java.lang.ClassCastException: com.mongodb.FindIterableImpl cannot be cast to com.mongodb.DBCursor.

Please help me to fix this.

1

There are 1 answers

1
evanchooly On

cursor should be a FindIterable. DBCursor is the old API. If collection is a MongoCollection you need to use the new API everywhere. Mixing and matching is almost certainly not going to work most of the time.