当前位置: 首页 > 知识库问答 >
问题:

Mongodb scala驱动程序不插入文档

鲁景山
2023-03-14

我尝试使用scala 2.11.7在mongodb 3.0.7中保存一个文档,然后进行快速访问:http://mongodb.github.io/mongo-scala-driver/1.0/gett-start/quick-tour/,但是我运行了这个示例,没有发生任何事情。不创建数据库、集合和文档。

  def main(args: Array[String]) {
    println("Start")
    val mongoClient: MongoClient = MongoClient()
    val database: MongoDatabase = mongoClient.getDatabase("mydb")
    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))
    val observable: Observable[Completed] = collection.insertOne(doc)
    observable.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")
    })

    mongoClient.close()
    println("End")
  }

控制台:

Start
dic 13, 2015 3:05:16 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
dic 13, 2015 3:05:16 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Opened connection [connectionId{localValue:1, serverValue:6}] to localhost:27017
dic 13, 2015 3:05:16 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 0, 7]}, minWireVersion=0, maxWireVersion=3, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=893710}
End

少了什么?

共有1个答案

陆光济
2023-03-14

我认为mongoclient.close()是在“observable”完成之前调用的。我想知道你如何用一种优雅的方式来解决这件事。但这不是一个真正的应用,我认为你只是测试一些。您可以将console.readline()放在mongoclient.close之前,因此当您看到inserted时,您可以按任意键,应用程序将结束。

 def main(args: Array[String]) {
    println("Start")
    val mongoClient: MongoClient = MongoClient()
    val database: MongoDatabase = mongoClient.getDatabase("mydb")
    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))
    val observable: Observable[Completed] = collection.insertOne(doc)
    observable.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")
    })
    scala.Console.readLine()
    mongoClient.close()
    println("End")
  }
 类似资料:
  • 我正在尝试编写一个Java函数,将单词列表插入到集合中。我想为每个单词的唯一字段“单词”一个文档。我要插入的单词列表包含许多重复的单词,所以我希望我的函数只在集合中没有具有相同“word”值的文档时才插入文档。如果已经有一个具有相同“单词”值的文档,该函数不应该改变或替换这个文档,而是继续插入我的列表中的下一个单词。 我在字段“word”上创建了一个索引,以避免重复的文档并捕获重复的键Except

  • 我试图使用liquibase gradle插件来生成数据库和Springboot实体之间的数据库差异。然而,我总是错误地说 液化。例外Liquibase异常:运行Liquibase:java时出现意外错误。lang.RuntimeException:找不到数据库驱动程序:class org。postgresql。驾驶员 根据一些google的结果,我把类路径的依赖关系放在构建脚本块中。但是没有用。

  • 我们正在尝试使用DataStax驱动程序将CSV文件中的数据插入Cassandra。有哪些方法可以做到这一点? 我们目前使用运行cqlsh从CSV文件加载。

  • 我有一个Cassandra的单节点实例。我一直在使用Java中的datastax驱动程序使用批处理语句向其中插入大量数据。 在插入过程中的某个点之后,我会看到一个NoHost可用异常,但是我仍然可以通过cql连接到节点并执行语句。Cassandra 日志警告我,批处理太大,当我将大小降低到建议的水平时,我仍然收到相同的错误,并且 Cassandra 日志文件中似乎没有其他错误。 以前有人遇到过这个

  • 问题内容: 有没有一种简单的方法可以使用Java驱动程序获取mongoDB实例最后插入的文档的ID(对象ID)? 问题答案: 我刚刚意识到您可以这样做:

  • [debug]应用程序-update(){“_id”:{“$OID”:“5759542A4E0BF602ADCAB149”},“title”:“文本平铺”,“信誉”:0} [debug]application-org.mongodb.scala.observableImplicits$boxedobservable@61ddc581 [debug]application-onsubscribe:o