简体   繁体   English

Mongodb Scala驱动程序未插入一个文档

[英]Mongodb scala driver doesn't insertOne document

I try to save a document in mongodb 3.0.7 using scala 2.11.7 following the quick tour: http://mongodb.github.io/mongo-scala-driver/1.0/getting-started/quick-tour/ But I run the example and nothing happen. 在快速浏览后,我尝试使用scala 2.11.7在mongodb 3.0.7中保存文档: http ://mongodb.github.io/mongo-scala-driver/1.0/getting-started/quick-tour/但我运行这个例子,什么也没有发生。 The database, collection and document are not created. 没有创建数据库,集合和文档。

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

Console: 安慰:

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

What is missing? 什么东西少了?

I think the mongoClient.close() is being called before the 'Observable' is completed. 我认为mongoClient.close()在“可观察”完成之前被调用。 I'm wondering how you could solve this with an elegant way. 我想知道您如何用一种优雅的方式解决这个问题。 But regarding this is not a real applicaton and I think you are just testing some. 但是,这并不是一个真正的应用,我认为您只是在测试一些。 You can put Console.readLine() before mongoClient.close so when you see Inserted you can press any key and the application will end. 您可以将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")
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB不会在集合中插入第二个文档(C#驱动程序) - MongoDB doesn't insert a second document into a collection (C# Driver) mongodb scala驱动程序-无法检索文档 - mongodb scala driver - unable to retrieve a document MongoDB Scala 驱动程序不允许在 case 类中使用 Map 集合 - MongoDB Scala Driver doesn't allow use Map collection in case class MongoDB golang驱动InsertOne时间从何而来? - Where does the MongoDB golang driver InsertOne time come from? go.mongodb.org/mongo-driver-具有NilValueObjectId的InsertOne - go.mongodb.org/mongo-driver - InsertOne with NilValueObjectId MongoWriteException:InsertOne 将 MongoDB.Driver.MongoBulkWriteException 作为内部异常 - MongoWriteException: InsertOne has MongoDB.Driver.MongoBulkWriteException as inner exception 从 mongodb scala 驱动程序 2.4.2 读取文档数组到地图 - Reading document array to a map from mongodb scala driver 2.4.2 使用 C# 驱动程序(MongoDB)更新完整文档(如果“Id”存在则替换所有文档//如果“Id”不存在则插入) - Upsert a complete document (replace all if "Id" exists // insert if "Id" doesn't exists ) with C# Driver (MongoDB) nodejs mongodb - 如何使用insertOne返回插入的文档 - nodejs mongodb - how to return the inserted document using insertOne NodeJS + MongoDB:insertOne() - 从 result.ops 中获取插入的文档 - NodeJS + MongoDB: insertOne() - get the inserted document from result.ops
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM