We are building a new experience for our SmartBear Community.
This site is now read-only. You can still browse and search as usual. Learn More
This site is now read-only. You can still browse and search as usual. Learn More
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();
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();
Hi,
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,
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.
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.
i.e.
collection.each {
Document myDoc = it;
... do something with individual document ...
};
Regards,
Todd