4

I have some documents in MongoDB like the following:

{
  "no" : "ABC123",
  "description": "The brown fox jumped over the lazy dog"
}

I want to be able to search through all such documents in the collection and return all documents which contain, say, the word "fox" in the description. Is this possible with ReactiveMongo? Thanks

cchantep
  • 9,118
  • 3
  • 30
  • 41
saintlyRook
  • 475
  • 1
  • 4
  • 13

2 Answers2

4

Actually, the problem you're trying to solve has nothing to do with ReactiveMongo as a driver.

For example, you can use $regex command to search string in mongodb like this:

def coll: JSONCollection = db collection "your_collection"

val nameToFind = "Andrey"
val query = obj("name" -> obj("$regex" ->  (".*" + nameToFind + ".*")))
coll.find(query).cursor[JsObject].collect[List]() map {
  case objects => Ok(obj("result" -> objects))
}
Andrey Neverov
  • 2,135
  • 1
  • 12
  • 21
2

This can be done with the $text operator.