无法使用scala驱动程序将数据插入mongo

时间:2016-08-09 23:47:48

标签: mongodb scala

我无法使用scala驱动程序将数据保存到Mongo

简而言之,代码完成,但数据永远不会插入到Mongo中。 Observer是创建和订阅的,但后来没有任何反应。

这是我的代码的副本

JPanel

我的程序输出是:

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()
 }
}

当我的程序运行(或挂起)时,mongo表示我的程序已连接,但 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 未显示任何待处理的写操作。

我还尝试将 writeConcern 设置为已确认 W1 ,但这似乎也没有做任何事情。

我也在使用mongod版本3.2.6和mongo-scala-driver版本1.0.1

2 个答案:

答案 0 :(得分:1)

是的,最初也让我感到困惑。

onSubscribe方法中,您获得了一个Subscription对象。当您准备接收(观察)数据时,在其上调用.request()。在您的情况下,您可以使用Long.MAX_VALUE方法中的onSubscribe()来调用它。

答案 1 :(得分:0)

我最终使用ax = gca; ax.Position = [left bottom width height]; 并按照github axes上的驱动程序示例

设法使其工作