I am also facing the same issue and below is the complete code i am using in JMeter tool-
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.types.ObjectId;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Updates.*;
// MongoDB connection details
def serverAddress = new ServerAddress(vars.get("mongoHost"), vars.get("mongoPort").toInteger())
// MongoDB credential information
def username = vars.get("mongoUsername");
def databasename = vars.get("mongoDatabase");
def password = vars.get("mongoPassword").toCharArray()
def credentials = MongoCredential.createCredential(username, databasename, password)
// Create MongoClient
def mongoClient = new MongoClient(serverAddress, [credentials] as List)
// Connect to the database
def database = mongoClient.getDatabase(databasename)
// Access a collection
def collection = database.getCollection(vars.get("mongoCollection"))
// Document to find
Document result = collection.find(eq("email", vars.get("mail"))).first();
//Document result = collection.await collection.find({});
if (result != null)
{
// Document to update
//insertOne
collection.updateOne(eq("email", vars.get("randomEmail")),combine(set("isActive", true)));
return "User with email=" + vars.get("email") + " modified";
//return result;
}else
{
return "No Record Found";
}
// Close MongoDB connection
mongoClient.close()
**error message-**
Response code:500
Response message:javax.script.ScriptException: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=mongo --host effie-sharedperf-docdb.cluster-cpmjeylhxh2g.us-east-1.docdb.amazonaws.com:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: mongo --host effie-sharedperf-docdb.cluster-cpmjeylhxh2g.us-east-1.docdb.amazonaws.com}, caused by {java.net.UnknownHostException: mongo --host effie-sharedperf-docdb.cluster-cpmjeylhxh2g.us-east-1.docdb.amazonaws.com}}]
SampleResult fields:
ContentType:
DataEncoding: null