Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find by regular expression with Casbah

how to use regular expressions at Collection#find(/* HERE */) like:

val coll = MongoConnection()("foo")("bar")
for(x <- coll.find("name" -> ".*son$".r)) {
   // some operations...
}
like image 595
singaan Avatar asked Feb 03 '11 15:02

singaan


2 Answers

You are close, you just need to wrap your conditions in a MongoDBObject().

We had to pull out the implicit conversions of <key> -> <value> in a bunch of places because they were hard to catch properly and were breaking other code.

They'll probably be back in 2.1.

Do this instead:

val coll = MongoConnection()("foo")("bar")
for(x <- coll.find(MongoDBObject("name" -> ".*son$".r))) {
   // some operations...
}
like image 186
Brendan W. McAdams Avatar answered Nov 12 '22 21:11

Brendan W. McAdams


For adding IGNORECASE above answer will not work by appending "/i" at the end of regex in Scala, Casbah. For this purpose use:

val EmailPattern = Pattern.compile(companyName,Pattern.CASE_INSENSITIVE)
val q = MongoDBObject("companyName" ->  EmailPattern)
val result = MongoFactory.COLLECTION_NAME.findOne(q)
like image 36
Nilesh Avatar answered Nov 12 '22 20:11

Nilesh