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
object MongoTest extends Appfor scala to know this is main function you must extendAppmainmethod. @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?state=CONNECTEDonNext. 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 thelatch?onNextbut it never gets called.