Finding text in MongoDB with ReactiveMongo

I have some documents in MongoDB as shown below:

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

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

+4
source share
2 answers

In fact, the problem you are trying to solve is not related to ReactiveMongothe quality of the driver.

For example, you can use the command $regexto search for a string in mongodb as follows:

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))
}
+4
source

This can be done using the $ text operator .

+2
source

Source: https://habr.com/ru/post/1537002/


All Articles