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 toMongoDB, 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())Solved10KViews0likes5CommentsHow 'find' command to be used to fetch results from mongoDB using groovy in soap ui
Below is the groovy code snippet I have written. DB db = mongoClient.getDB( "qahmedb" ); DBCollection coll = db.getCollection("Hotel"); log.info "Collection Hotel selected successfully" DBCursorcursor = coll.find({City_en : 'Orlando' }); List list1 = new ArrayList(); while( cursor.hasNext() ) { def hotel = cursor.next() log.info "Found Hotels with ID $hotel._id and name $hotel.Title_en" } It is leading to error message as "ERROR:An error occurred [Cannot cast object 'DBCollection{database=DB{name='qahmedb'}, name='Hotel'}' with class 'com.mongodb.DBCollection' to class 'com.mongodb.DBCursor'], see error log for details" While if I am using direct find query without any condition likeDBCursorcursor = coll.find(); It gave results successfully without any error. Kindly help with the possible solutions. Thanks Deepali3.4KViews0likes7CommentsMongo Context varibale - Is it possible to use it throughout the test suite
Greetings, While working with MondoDB using gmongo (groovy wrapper), we open connection using mongoDB context, something like this: def mongoClient = new MongoClient( new ServerAddress(dbServer), Arrays.asList(credentials) ) context.gmongo = new GMongo( mongoClient ) context.mongoDB = context.gmongo.getDB(dbDatabase) In my current framework, I open connection and close it everytime I need to do something with mongodb. I am trying to somehow define the context once globally and use the variable to access mongodb throughout the test suite. Is there any way to do it ? Any help would be much appreciated.Solved3KViews0likes8Comments