3

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

Odo
  • 209
  • 1
  • 12
  • Here is a part of my data that i query: case class Machine(_id: ObjectId = new ObjectId, serverName: Option[String], – Odo Jan 13 '12 at 09:09
  • Ok. seems that i fixed it. 'val query = "serverName" -> regexp.r' – Odo Jan 13 '12 at 09:35
  • The next thing that i'm doing wrong is the query on a List with an regexp. Field is: `ips: List[String] = List[String](),` If tried it with: `val regexp = ".*" + parameter + ".*" val nameQ = "serverName" -> regexp.r val ipsQ = "ips" $elemMatch regexp.r val query = $or (("serverName" -> regexp.r), ("ips" $elemMatch regexp.r))` But this breaks the signature with: `No implicit view available from scala.util.matching.Regex => com.mongodb.DBObject` – Odo Jan 13 '12 at 11:11

1 Answers1

1

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))
RameshVel
  • 64,778
  • 30
  • 169
  • 213
  • 1
    I think you do not need to provide the /es. For me it works now with `".*" + serverName + ".*"` and `val result = find(MongoDBObject("serverName" -> regexp.r))` – Odo Jan 13 '12 at 11:14