Casbah/Salat: How to query a field that a part of a string is contained?

1.5k views Asked by At

i try to write a query with Casbah and Salat to query a field that it includes parts of a name. I tried to use a regular expression like this (inside a SalatDAO):

val regexp = (""".*"""+serverName+""".*""").r
val query = "serverName" -> regexp
val result = find(MongoDBObject(query))

and with

val regexp = ".*"+serverName+".*"

The record is in MongoDB and when i search it with the complete name it works.

How is the right way to tell casbah to search for a part of the string ?

Another thing that i would like to fix is the string concatenation for the parameter. Is there any default way to escape input parameters with casbah, so the parameter is not interpreted as a javascript command ?

Best Regards, Oliver

1

There are 1 answers

1
RameshVel On BEST ANSWER

In mongodb shell you can find the server names contains the specific string by

db.collection.find({serverName:/whatever/i})

i dont have any experience with casbah, i believe it must be like this. please test

val regexp = ("""/"""+serverName+"""/i""").r  
find(MongoDBObject("serverName" -> regexp))