Search code examples
scalaplayframeworkplay-reactivemongo

find with reactive mongo and play framework


I want to retrieve a user's data by his mail from MongoDB using ReactiveMongo driver for play framework but it returns: Future(<not completed>)

Here is my code:

def findBymail(email: String) = {
  val query = Json.obj("mail" -> email)
  val resul = collection.flatMap(_.find(query).one[Users])
  Logger.warn(s"result found: $res") 
}

Solution

  • All operations in ReactiveMongo is async, it always returns Future, So you can print the result with

    collection.flatMap(_.find(query).one[Users]).map{ u => Logger.warn(s"result found: $res")
    

    I think this may not you want, you can return the Future as well, and handle the result,

    def findBymail(email: String) = {
      val query = Json.obj("mail" -> email)
      collection.flatMap(_.find(query).one[Users]).map{ user =>
          Logger.warn(s"result found: $user") 
          user
      }
    }
    

    You can handle the result as:

    findBymail("....").map{ user =>
        ......
    }