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.
cursor
should be aFindIterable
.DBCursor
is the old API. Ifcollection
is aMongoCollection
you need to use the new API everywhere. Mixing and matching is almost certainly not going to work most of the time.