Retreiving data from Mongo db using Groovy and assigning it to a test case property
Hi, I'm looking to create a test step that can connect to MongoDB, run a query, and then assign the result to a test case property. I have the connection working and I can retrieve a result as a row count. However I cannot get the actual result to assign it to a test case property. I know how to assign a value to a test case property but simply cannot get the result in order to do that. Below is my existing code. I'm connecting to a MongoDB called database called 'digital', within that there is a collection called 'QuoteSummary'. Within 'QuoteSummary' I'm searching for a "quoteRefNo" where "workReferenceNo" = GPE0024879. Its the resulting "quoteRefNo" that I want to assign to a test case property.
import com.gmongo.GMongoClient
import com.mongodb.MongoCredential
import com.mongodb.ServerAddress
import com.mongodb.BasicDBObject
def credentials = MongoCredential.createCredential('hidden_user', 'digital', 'hidden_pwsd' as char[])
def serverAddress = new ServerAddress('hidden_servername_and_port')
def mongo = new GMongoClient(serverAddress, [credentials])
def db = mongo.getDB('digital')
BasicDBObject query = new BasicDBObject("workReferenceNo":"GPE0024879")
BasicDBObject query1 = new BasicDBObject(quoteRefNo: 1,_id: 0)
def collection = db.getCollection('QuoteSummary')
//log.info(collection.find(query).count())
log.info(collection.find((query),(query1)).count())
hello martinrampton,
that query looks a little out of place... Maybe just stick with the collection to debug for a little bit then add the query.
.
.
def db = mongo.getDB('digital')
def collection = db.getCollection('QuoteSummary')log.info collection.find().first();
// or
Document myDoc = collection.find().first();
log.info myDoc.toString();
// or
log.info myDoc.toJson();
..
if you can identify content in the collection, you can start forming query.
i.e.
myDoc = collection.find(eq("workReferenceNo", "GPE0024879")).first();
log.info myDoc.toString();
// or
log.info myDoc.toJson();