如何获取mongo db数据并传入jmeter请求

时间:2017-08-08 06:26:47

标签: mongodb groovy jmeter jmeter-3.2

我有一个帖子调用,响应是这样的。

{
  "status":0,
  "message":"Prescription Created",
  "jsonResponse":{},
  "cid":"C5975K",
  "pid":"Rx5975K-175A",
  "prescriptionSource":"GO_RX_CTO",
  "imageStatus":[]
}

通过这个pid,我必须再查询一条记录。例如:

db.order.find({"pid":"Rx5975K-175A"})

,此查询的结果应再传递一个jmeter请求。

我使用过MongoDB脚本(DEPRECATED)..但是这不会被弃用了。

尝试使用JSR223采样器,但它不适用于新的jmeter 3.2

import com.mongodb.*
import com.mongodb.BasicDBObject

    MongoCredential coreCredential = MongoCredential.createCredential("${mongodb_user}", "${mongodb_database}", "${mongodb_password}".toCharArray());
    MongoClient coreMongoClient = new MongoClient(new ServerAddress("${mongodb_server}", 13017), Arrays.asList(coreCredential));
    DB coreDB = coreMongoClient.getDB("${mongodb_database}");
    DBCollection coll = coreDB.getCollection("order");
    coll.find();

1 个答案:

答案 0 :(得分:0)

你必须根据“pid”找到你的结果,你无处可去。找到您的集合后,您需要创建一个查询并使用该查询进行搜索。

import com.mongodb。* import com.mongodb.BasicDBObject

MongoCredential coreCredential = MongoCredential.createCredential("${mongodb_user}", "${mongodb_database}", "${mongodb_password}".toCharArray());
MongoClient coreMongoClient = new MongoClient(new ServerAddress("${mongodb_server}", 13017), Arrays.asList(coreCredential));
DB coreDB = coreMongoClient.getDB("${mongodb_database}");
DBCollection coll = coreDB.getCollection("order");
BasicDBObject query = new BasicDBObject();
query.put("pid", "Rx5975K-175A");
DBObject  getData= coll.findOne(query);

这将给你想要的结果