0

I am using Groovy to query on MongoDB and I am getting not authorized in querying in MongoDB. Can you please check if there is a problem on my script?

DBName default Collection paymentHeader

import com.mongodb.*;
import com.mongodb.DB;
import com.mongodb.MongoCredential;
import org.apache.jmeter.protocol.mongodb.config.MongoDBHolder;
import com.mongodb.BasicDBObject; 
import com.mongodb.DBCollection;

//Get DB
DB db = MongoDBHolder.getDBFromSource("SITDB","${DBName}","${username}","${password}");
boolean auth = db.authenticate("${username}","${password}".toCharArray());

//Get Collection
DBCollection collection = db.getCollection("${Collection}");

//Find ApplicationNum
//collection.find({applicationNum: "${applicationId}"});
BasicDBObject query = new BasicDBObject("applicationNum", "${applicationId}");
DBObject result = collection.findOne(query);
SampleResult.setResponseData(result.toString().getBytes());

Response code: 500 Response message: javax.script.ScriptException: com.mongodb.MongoException: not authorized for query on default.paymentHeader

1 Answer 1

1

This doesn't have anything with JMeter, you need to provide read permissions for the ${username} onto default.paymentHeader collection.

Also be aware that you should not reference JMeter Functions or Variables as ${username}, you should use vars shorthand for JMeterVariables class instead like vars.get('username')

More information: JSR223 Sampler Documentation, in particular:

JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.

props.get("START.HMS");

props.put("PROP1","1234");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.