Forum Discussion
Thanks Rao for your help...but I have already tried this...but no luck.
- Deepali9 years agoOccasional Contributor
import com.mongodb.*
import com.mongodb.MongoCredential as MC
import com.mongodb.ServerAddress
def credential = MC.createMongoCRCredential( "<username>",
"<DBname>",
"<pwd>".toCharArray() )
def mongoClient = new MongoClient( new ServerAddress("<server sddress>"), [ credential ] )log.info "Connect to database successfully"
DB db = mongoClient.getDB( "qahmedb" );
DBCollection coll = db.getCollection("Hotel");
log.info "Collection Hotel selected successfully"DBCursor cursor = 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"
}Stack trace:
2016-01-25 15:56:32,000 ERROR [errorlog] org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'DBCollection{database=DB{name='qahmedb'}, name='Hotel'}' with class 'com.mongodb.DBCollection' to class 'com.mongodb.DBCursor'
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'DBCollection{database=DB{name='qahmedb'}, name='Hotel'}' with class 'com.mongodb.DBCollection' to class 'com.mongodb.DBCursor'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
at Script43.run(Script43.groovy:15)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:92)
at com.eviware.soapui.support.scripting.groovy.SoapUIProGroovyScriptEngineFactory$SoapUIProGroovyScriptEngine.run(SoapUIProGroovyScriptEngineFactory.java:76)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:155)
at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:263)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)With condition City_en : 'Orlando', it has 3 records, while in total it has 500+ records.
It gives successful result when done without any condition in find query.
- Deepali9 years agoOccasional Contributor
Any help would be great regarding the above problem.
Thanks
Deepali
- Harshini7 years agoNew Contributor
Hi Deepali,
Query has to used as DB object when you put to find() method. Hope below query helps
Try the below code
try
{
BasicDBObject allQuery = new BasicDBObject();
allQuery.put("_id", "000000000001093467822SO01");DBCursor cursor =dbColl.find(allQuery);
//DBCursor cursor =dbColl.find();
JSON json =new JSON();
String serialize = json.serialize(cursor);
System.out.println(serialize);
//List list1 = new ArrayList();
while(cursor.hasNext())
{
def var = cursor.next()
log.info "Party id $var"
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}Harshini Kasa
- Harshini7 years agoNew Contributor
Hi Deepali,
Query has to used as DB object when you put to find() method. Hope below query helps
Try the below code
try
{
BasicDBObject allQuery = new BasicDBObject();
allQuery.put("_id", "000000000001093467822SO01");DBCursor cursor =dbColl.find(allQuery);
//DBCursor cursor =dbColl.find();
JSON json =new JSON();
String serialize = json.serialize(cursor);
System.out.println(serialize);
//List list1 = new ArrayList();
while(cursor.hasNext())
{
def var = cursor.next()
log.info "Party id $var"
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Related Content
- 2 years ago
Recent Discussions
- 5 days ago
- 10 days ago