Search code examples
mongodbscalamongo-scala-driver

Mongo Scala Driver - Can't insert in the database


I'm practicing on a project that needs a database connection, I'm using the Play Framework combine to Scala and MongoDB.

I'm also using Mongo-scala-driver and following the documentation. I wrote the exact same code:

    println("start")

    val mongoClient: MongoClient = MongoClient("mongodb://localhost:27017/Sandbox")

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

    val collection: MongoCollection[Document] = database.getCollection("test")

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

    collection.insertOne(doc).subscribe(new Observer[Completed] {

      override def onSubscribe(subscription: Subscription): Unit = println("Subscribed")

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

      override def onError(e: Throwable): Unit = println("Failed")

      override def onComplete(): Unit = println("Completed")
    })

    mongoClient.close()

    println("end")

Nothing is inserted into the database and the only result i get from the log is this:

start
Subscribed
end

I've been looking on stackoverflow for similar subject but everything I found didn't work for me.


Solution

  • You try insert document in asyncronous mode. Therefore you must define three call back function onNext onError and onComplete But you don't give time for execute insertion.

    Try append any timeout before close connection. For example simple add

    Thread.sleep(1000)
    

    before

    mongoClient.close()
    

    And you no need redefine onSubscribe()

    if you not want manually control demand when you move in documents list from you requests then you no need override onSubscribe(). The default definition for onSubscrime() very usable for trivial requests. In you case you no need override him.

    The next code is worked

    println("start")
    val mongoClient: MongoClient = MongoClient("mongodb://DB01-MongoDB:27017/Sandbox")
    val database: MongoDatabase = mongoClient.getDatabase("test")
    val collection: MongoCollection[Document] = database.getCollection("test")
    val doc: Document = Document("_id" -> 0,
                                 "name"  -> "MongoDB",
                                 "type"  -> "database",
                                 "count" -> 1,
                                 "info"  -> Document("x" -> 203, "y" -> 102))
    collection
      .insertOne(doc)
      .subscribe(new Observer[Completed] {
        override def onNext(result: Completed): Unit = println("Inserted")
        override def onError(e: Throwable): Unit     = println("Failed")
        override def onComplete(): Unit              = println("Completed")
      })
    
    Thread.sleep(1000)
    mongoClient.close()
    println("end")
    

    }