0

I am on Ubuntu 20.04. I want to write some data in Scala to MongoDB. Here's what I have:

import org.mongodb.scala.bson.collection.immutable.{Document => MongoDocument}
import org.mongodb.scala.{MongoClient, MongoCollection, MongoDatabase}

object Application extends App {
  val mongoClient: MongoClient = MongoClient()

  // Use a Connection String
  //val mongoClient: MongoClient = MongoClient("mongodb://localhost")

  val database: MongoDatabase = mongoClient.getDatabase("mydb")

  val collection: MongoCollection[MongoDocument] = database.getCollection("user")

  val doc: MongoDocument = MongoDocument("_id" -> 0, "name" -> "MongoDB", "type" -> "database",
  "count" -> 1, "info" -> MongoDocument("x" -> 203, "y" -> 102))
  collection.insertOne(doc)

  val documents = (1 to 100) map { i: Int => MongoDocument("i" -> i) }
  collection.insertMany(documents)
}

The error (not even an error, INFO level) I get:

Nov 16, 2020 1:42:08 AM com.mongodb.diagnostics.logging.JULLogger log

INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}

And nothing happens to the database. No data appears there. No errors, no insertions into Mongo, nothing.

I used primarily these sources as examples:

https://mongodb.github.io/mongo-scala-driver/2.9/getting-started/quick-tour/

https://blog.knoldus.com/how-scala-interacts-with-mongodb/

MongoDB is up, status is active. Inserting data from the terminal was done successfully. So, the program's behavior I have is strange. I've been searching everywhere on the Internet for the answers but I can't seem to find it. Your help will be appreciated a lot. Thank you!

George Zorikov
  • 139
  • 1
  • 4
  • 8
  • 1
    So your operations return **Obersever** which haven't been subscribed to anything they simply won't be executed. The simplest solution is to turn them into **Futures** and then adding an `Await` to block that thread until the operation is finished. – Luis Miguel Mejía Suárez Nov 15 '20 at 23:26
  • @LuisMiguelMejíaSuárez, thank you! Yes, that was the problem – George Zorikov Nov 16 '20 at 09:08

1 Answers1

0

Thanks to @Luis Miguel Mejía Suárez's help. Here's what I have done so far: added an Observer implementation and a promise. Thanks to this post: Scala script wait for mongo to complete task . That's what I have now:

    val mongoClient: MongoClient = MongoClient("mongodb://localhost")
    val database: MongoDatabase = mongoClient.getDatabase("mydb")
    val collection: MongoCollection[MongoDocument] = database.getCollection("user")

    val doc: MongoDocument = MongoDocument("name" -> "MongoDB", "type" -> "database",
      "count" -> 1, "info" -> MongoDocument("x" -> 203, "y" -> 102))
    val observable: Observable[Completed] = collection.insertOne(doc)

    val promise = Promise[Boolean]
    observable.subscribe(new Observer[Completed] {

      override def onNext(result: Completed): Unit = println("Inserted")

      override def onError(e: Throwable): Unit = {
        println("Failed")
        promise.success(false)
      }

      override def onComplete(): Unit =  {
        println("Completed")
        promise.success(true)
      }
    })

    val future = promise.future
    Await.result(future, Duration(5, java.util.concurrent.TimeUnit.SECONDS))

    mongoClient.close()

Generally speaking, it works in most cases. Though, I didn't handle the case with insertMany method where the program has to wait for the last element insertion. My realization does not work properly with this.

P.S. Turns out insertMany also works fine with this example, I just tested it with the wrong data.

George Zorikov
  • 139
  • 1
  • 4
  • 8