Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 582
Odo Avatar asked Jan 20 '26 00:01

Odo


1 Answers

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))
like image 118
RameshVel Avatar answered Jan 21 '26 12:01

RameshVel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!