scrineym
scrineym

Reputation: 759

Cannot insert data into mongo using scala driver

I'm unable to save data into Mongo using the scala driver

In short, the code completes, but the data is never inserted into Mongo. The Observer is created and subscribed, but then nothing happens.

Here's a copy of my code

object MongoTest {
  def main(args: Array[String]) {
  val SERVER = "127.0.0.1"
  val DATABASE="mytest"
  val connection = MongoClient("mongodb://"+SERVER)
  val database = connection.getDatabase(DATABASE)

  var items:List[Document] = List[Document]()
  for(i <- 1 to 10){
    items = items:+ Document("_id"->new ObjectId(),"val"->i)//generate dummy data
  }

  val latch = new CountDownLatch(1)
  val db  =  database.getCollection("testInsert")
  db.insertMany(items).subscribe(new Observer[Completed] {

     override def onError(e: Throwable): Unit = {
        println("Error")
        e.printStackTrace()
     }

     override def onSubscribe(subscription: Subscription): Unit = {
       println("Exporting")
     }

     override def onComplete(): Unit = {
       println("Completed")
       connection.close()
       latch.countDown()
     }

     override def onNext(result: Completed): Unit = {
        println("Next")
     }

    })
    latch.await()
 }
}

The output of my program is:

 INFO [main] (SLF4JLogger.java:71) - Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
DEBUG [main] (SLF4JLogger.java:56) - Updating cluster description to  {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING}]
 INFO [cluster-ClusterId{value='57ab00600aa9452d90826eb8', description='null'}-127.0.0.1:27017] (SLF4JLogger.java:71) - Opened connection [connectionId{localValue:1, serverValue:2}] to 127.0.0.1:27017
DEBUG [cluster-ClusterId{value='57ab00600aa9452d90826eb8', description='null'}-127.0.0.1:27017] (SLF4JLogger.java:56) - Checking status of 127.0.0.1:27017
INFO [cluster-ClusterId{value='57ab00600aa9452d90826eb8', description='null'}-127.0.0.1:27017] (SLF4JLogger.java:71) - Monitor thread successfully connected to server with description ServerDescription{address=127.0.0.1:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 8]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=1163475}
DEBUG [cluster-ClusterId{value='57ab00600aa9452d90826eb8', description='null'}-127.0.0.1:27017] (SLF4JLogger.java:56) - Updating cluster description to  {type=STANDALONE, servers=[{address=127.0.0.1:27017, type=STANDALONE, roundTripTime=1.2 ms, state=CONNECTED}]
Exporting

When my program is running (or hanging) mongo says my program is connected, but db.currentOp(1) doesn't show any pending write operations.

I have also tried setting the writeConcern to Acknowledged and W1 but this doesn't seem to do anything either.

Also I am using mongod version 3.2.6 and the mongo-scala-driver version 1.0.1

Upvotes: 1

Views: 813

Answers (2)

Guus Bloemsma
Guus Bloemsma

Reputation: 31

Yep, that baffled me too initially.

In the onSubscribe method you get a Subscription object. Call .request() on it when you are ready to receive (observe) data. In your case you can just call it with Long.MAX_VALUE from within your onSubscribe() method.

Upvotes: 1

scrineym
scrineym

Reputation: 759

I managed to get it working in the end by using bulkWrite and following the example for the driver on github here

Upvotes: 0

Related Questions