1

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

5
  • How are you executing this program? object MongoTest extends App for scala to know this is main function you must extend App Commented Aug 10, 2016 at 4:47
  • He has a main method. @nishnet2002 Do you have a working instance of mongo at the time of running the code? Do you get any connection warnings? Does it output anything at all? Commented Aug 10, 2016 at 7:27
  • @sebszyller, sorry,just added in the ouput of log4j, it says it connects successfully from what I can tell , the last debug message says state=CONNECTED Commented Aug 10, 2016 at 10:27
  • One more thing that could be useful - add some logging onNext. As it stands all we know from the log is that you connected to mongo and that you subscribed to the action. Another thing, does it work without the latch? Commented Aug 10, 2016 at 10:48
  • @sebszyller without the latch the program just exits and closes the connection. I've added in a print statement in onNext but it never gets called. Commented Aug 10, 2016 at 11:25

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.