Ask a Question

Retreiving data from Mongo db using Groovy and assigning it to a test case property

Occasional Contributor

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')

Champion Level 2

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') collection.find().first();

// or

Document myDoc = collection.find().first(); myDoc.toString();

// or myDoc.toJson();


if you can identify content in the collection, you can start forming query.


myDoc = collection.find(eq("workReferenceNo", "GPE0024879")).first(); myDoc.toString();

// or myDoc.toJson();

SmartBear Alumni (Retired)



@martinrampton, do you have any updates on the case?

Did you have a chance to try the suggestion that @TNeuschwanger provided?


If it answers your question, please click the Accept as Solution button below that suggestion.



We are looking forward to hearing from you,


Olga Terentieva
SmartBear Assistant Community Manager

Thanks Todd, I worked though this in debug as you suggested and eventually manged to get some meaningful data out of Mongo DB. I now have it passing this data back into my test case.


I used the below code you have mentioned

Document myDoc=collection.find().first()

If I used first() or last() function it works fine. I pull the first or last document from the collection. However if I try to use just find() I’m not able to retrieve ALL the documents that match the query I pass in the file. Can you please help? What is the code to pull all the docs from the collection.

Hello Karsa,

I don't know the answer...  I would look at the ".each" on a collection.  I am only speculating that you could use similar iteration techniques that are used on lists or maps.


collection.each {

   Document myDoc = it;

   ...  do something with individual document  ...   





Showing results for 
Search instead for 
Did you mean: