1

I have stored ObjectId("5e0f216aab9234688b125ac1") in a variable i-e complete value, and in following code iam trying to access EmployeeOID which is an integer

try {
    MongoCollection<Document> collection = vars.getObject("collection");
Document result = collection.find(eq("_id",'completeValue').first());
log.info (result)
if(result !=null){

vars.put ("Employeeoid", result.get("EmployeeOID").tointeger());
log.info (Employeeoid)
return "Employeeoid=" + Employeeoid;
}

IAm getting this error:

Response code: 500
Response message: Exception: groovy.lang.MissingMethodException: No signature 
of method: com.mongodb.client.model.Filters$SimpleEncodingFilter.first() is 
applicable for argument types: () values: []
Possible solutions: print(java.io.PrintWriter), print(java.lang.Object), 
find(), find(groovy.lang.Closure), is(java.lang.Object), 
printf(java.lang.String, [Ljava.lang.Object;)
Sarwat Hussain
  • 127
  • 1
  • 12
  • try `findOne(...)` – injecteer Jan 03 '20 at 12:42
  • @injecteer No luck! Document result = collection.findone(eq("_id",'completeValue')) Error: Response code: 500 Response message: Exception: groovy.lang.MissingMethodException: No signature of method: com.mongodb.client.internal.MongoCollectionImpl.findone() is applicable for argument types: (com.mongodb.client.model.Filters$SimpleEncodingFilter) values: [Filter{fieldName='_id', value=completeValue}] Possible solutions: find(), find(), findAll(), find(com.mongodb.client.ClientSession), find(java.lang.Class), find(org.bson.conversions.Bson) – Sarwat Hussain Jan 03 '20 at 13:11

2 Answers2

0

I believe correct syntax would be:

Document result = collection.find(eq("_id", new ObjectId('5e0f216aab9234688b125ac1'))).first()

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • on using this Document result = collection.find(eq("_id",vars.get(new ObjectId("id")))) Iam getting this response: Response message: Exception: java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [id] – Sarwat Hussain Jan 07 '20 at 13:50
  • Who told you to use `collection.find(eq("_id",vars.get(new ObjectId("id"))))`, I might be suffering from a form of a mental fatigue, however I was under impression I suggested using `collection.find(eq("_id", new ObjectId('5e0f216aab9234688b125ac1')))` – Dmitri T Jan 07 '20 at 14:01
  • In my case the hexadecimal string is a dynamic thing actually which I am storing in variable named "id". I cant mention it this way as i don't know what it will be. – Sarwat Hussain Jan 08 '20 at 08:40
  • Then you need to use `collection.find(eq("_id",vars.get(new ObjectId(id))))`, Check out [Groovy Documentation](https://groovy-lang.org/documentation.html) and [Apache Groovy - Why and How You Should Use It](https://www.blazemeter.com/blog/groovy-new-black/) article to learn more about Groovy scripting. – Dmitri T Jan 08 '20 at 08:46
  • I tried: Document result = collection.find(eq("_id",vars.get(new ObjectId(id)))); but No luck! from the error seems like my sampler is unable to get the variable value from previous sampler( which is actually a value i could see in log) Error: Response code: 500 Response message: Exception: groovy.lang.MissingPropertyException: No such property: id for class: Script43 – Sarwat Hussain Jan 08 '20 at 09:19
  • It says that you don't have `id` variable defined. If you're having it in a JMeter Variable which you can access as `${id}` you can use `vars` shorcut in your script to read the value: `collection.find(eq("_id",vars.get(new ObjectId(vars.get('id')))))`. `vars` stands for [JMeterVariables](https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html)class instance, check out JavaDoc in particular and [Top 8 JMeter Java Classes You Should Be Using with Groovy](https://www.blazemeter.com/blog/top-8-jmeter-java-classes-you-should-be-using-with-groovy/) in general – Dmitri T Jan 08 '20 at 09:28
  • id is not a Jmeter variable in my case but i have stored it through following code in previous sampler: Document result = collection.find(eq("EmployeeOID",1111111)).first(); if(result !=null){ String ID =result.get("_id"); vars.put("id",ID); log.info( vars.get("id")) is there a way I can access this variable? – Sarwat Hussain Jan 08 '20 at 09:43
  • The way has been provided in the previous comment. – Dmitri T Jan 08 '20 at 09:45
  • On using this collection.find(eq("_id",vars.get(new ObjectId(vars.get('id'))))) Response code: 500 Response message: Exception: groovy.lang.MissingMethodException: No signature of method: org.apache.jmeter.threads.JMeterVariables.get() is applicable for argument types: (org.bson.types.ObjectId) values: [5e15b55f0f1421724d092efd] Possible solutions: get(java.lang.String), grep(), getAt(java.lang.String), grep(java.lang.Object), wait(), any() – Sarwat Hussain Jan 08 '20 at 11:00
0

Dont forget to import FindIterable! it works fine for me

FindIterable<Document> EmployeeOID = collection.find(eq("EmployeeOID ", new ObjectId("5e0f216aab9234688b125ac1")));
for (Document doc : EmployeeOID ) {
log.info(doc.get("_id").toString());
 }

[mongo][jmeter][groovy]