title: How to fetch data from mongodB in Scala
body: <p>I wrote following code to fetch data from MongoDB</p>
  
  <pre><code>import com.typesafe.config.ConfigFactory
  import org.mongodb.scala.{ Document, MongoClient, MongoCollection, MongoDatabase }
  
  import scala.concurrent.ExecutionContext
  
  object MongoService extends Service {
    val conf = ConfigFactory.load()
    implicit val mongoService: MongoClient = MongoClient(conf.getString("mongo.url"))
    implicit val mongoDB: MongoDatabase = mongoService.getDatabase(conf.getString("mongo.db"))
    implicit val ec: ExecutionContext = ExecutionContext.global
  
    def getAllDocumentsFromCollection(collection: String) = {
      mongoDB.getCollection(collection).find()
    }
  }
  </code></pre>
  
  <p>But when I tried to get data from <code>getAllDocumentsFromCollection</code> I'm not getting each data for further manipulation. Instead I'm getting</p>
  
  <pre><code>FindObservable(com.mongodb.async.client.FindIterableImpl@23555cf5)
  </code></pre>
  
  <p>UPDATED:</p>
  
  <pre><code>object MongoService {
    // My settings (see available connection options)
    val mongoUri = "mongodb://localhost:27017/smsto?authMode=scram-sha1"
  
    import ExecutionContext.Implicits.global // use any appropriate context
  
    // Connect to the database: Must be done only once per application
    val driver = MongoDriver()
    val parsedUri = MongoConnection.parseURI(mongoUri)
    val connection = parsedUri.map(driver.connection(_))
  
    // Database and collections: Get references
    val futureConnection = Future.fromTry(connection)
    def db1: Future[DefaultDB] = futureConnection.flatMap(_.database("smsto"))
    def personCollection = db1.map(_.collection("person"))
  
    // Write Documents: insert or update
  
    implicit def personWriter: BSONDocumentWriter[Person] = Macros.writer[Person]
    // or provide a custom one
  
    def createPerson(person: Person): Future[Unit] =
          personCollection.flatMap(_.insert(person).map(_ =&gt; {})) // use personWriter
    def getAll(collection: String) =
      db1.map(_.collection(collection))
  
    // Custom persistent types
    case class Person(firstName: String, lastName: String, age: Int)
  }
  </code></pre>
  
  <p>I tried to use reactivemongo as well with above code but I couldn't make it work for <code>getAll</code> and getting following error in <code>createPerson</code>
  <a href="https://i.stack.imgur.com/wkqnn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wkqnn.png" alt="enter image description here"></a>
  Please suggest how can I get all data from a collection.</p>