I'm using the mongodb-driver 3.5.0 with Eclipse, and I get no errors in the IDE but when I run my program I get:
Exception in thread "main" java.lang.NoSuchMethodError: com.mongodb.MongoClient.getDatabase(Ljava/lang/String;)Lcom/mongodb/client/MongoDatabase;
as an error. I've already checked and I'm only using 3.5.0 through a Maven dependency. For some reason everything before to get the MongoClient works fine, it's just getDatabase
that isn't working.
EDIT: I did a rough rebuild of the project using a gradle dependency instead of Maven, and that solved the issue. I'll keep looking into it to find the cause.
Example code below:
import org.bson.Document;
import org.bson.types.ObjectId;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.Block;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.*;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.client.model.ValidationOptions;
public static Document reportFind(String id, String destination) {
ObjectId objectId = ObjectId.get();
ObjectId provider_id = new ObjectId(id);
String provider_string = provider_id.toString();
String db = "loginapp";
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("loginapp");
MongoCollection<Document> coll = database.getCollection("reporttemplates");
System.out.println("Connected to database successfully");
Well according to the mongodb's current documentation the implementation of getDatabse is as:
This seems completely fine according to your implementation as well. I would hence invest time in investigating the build process for the project of which above is part of.