7

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

val coll = MongoConnection()("foo")("bar")
for(x <- coll.find("name" -> ".*son$".r)) {
   // some operations...
}
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
singaan
  • 83
  • 3

2 Answers2

14

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...
}
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
Brendan W. McAdams
  • 9,379
  • 3
  • 41
  • 31
1

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)
Nilesh
  • 2,054
  • 3
  • 23
  • 43