我正在尝试使用 servlet 上的 Java 驱动程序连接到托管在 mlab 上的 MongoDB 数据库。
import org.bson.Document; 
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class MongoConnection {
    protected void connectToMongo(String loc){
        String dbName = "readings";
        String collection = "data";
        MongoClientURI uri = new MongoClientURI("mongodb://user:pass@ds143109.mlab.com:43109/readings");
        MongoClient client = new MongoClient(uri);
        MongoDatabase db = client.getDatabase(dbName);
        MongoCollection<Document> readings = db.getCollection(collection);
        Document doc = Document.parse(loc);
        readings.insertOne(doc);
        client.close();
    }
}
Run Code Online (Sandbox Code Playgroud)
问题是我收到以下错误:
java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI
我查看了一个答案(How to resolve ClassNotFoundException: com.mongodb.connection.BufferProvider?),向我强调我需要其他 jars,我已经下载了它们,但是我仍然收到这个错误。
我正在使用 Eclipse 并将三个 jar 添加到构建路径中,通过右键单击项目然后按照Build Path -> …