NoSuchMethodError: com.mongodb.MongoNamespace.checkDatabaseNameValidity error in jmeter while trying to do performance test on mongoDB collection

Viewed 17

I am trying to connect to mongo collection to perform some load tests on it. but getting some error.

Can anyone please help me with the same?

DatabaseName and CollectionName I have stored in user defined variables.

Code I am using:

import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;

// Connect to the client
try {
    
    MongoClient mongoClient = MongoClients.create("mongodb://<userName>:<password>@prodoperationalcluster1-shard-00-00-pri.z1wts.mongodb.net:27017,prodoperationalcluster1-shard-00-01-pri.z1wts.mongodb.net:27017,prodoperationalcluster1-shard-00-02-pri.z1wts.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-44ekvq-shard-0&authSource=admin&retryWrites=true&w=majority");
    
    MongoDatabase database = mongoClient.getDatabase(vars.get("databaseName"));
    MongoCollection<Document> collection = database.getCollection(vars.get("collectionName"));  

    vars.putObject("collection", collection);
    
    return "Connected to " + vars.get("collectionName");    
}
catch (Exception e) {
    SampleResult.setSuccessful(false);
    SampleResult.setResponseCode("500");
    SampleResult.setResponseMessage("Exception: " + e);
}

1 Answers

In order to help you with the same we need to know what the same MongoDB java drivers you have in JMeter Classpath because if for example you downloaded the same driver .jar and BSON jar is not the same you will get the same error.

You can see Jar Hell for the same

Also ensure that the versions of MongoDB Java Client drivers and the version of MongoDB on the server side are the same because you cannot use the versions which are not the same for the same.

Check out MongoDB Performance Testing with JMeter article for more information on the same.

Related