我正在使用 Scala、Mongo DB 和 mongo-scala 驱动程序简单示例将数据插入 Mongo。
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "2.9.0"
// step 1 : connect to mongo
val mongoClient: MongoClient = MongoClient("mongodb://localhost:27017")
// step 2 : connect to db
val database: MongoDatabase = mongoClient.getDatabase("mydb")
// step 3 : get the collection
val collection: MongoCollection[Document] = database.getCollection("test")
// step 4 : create document
val doc: Document = Document("_id" -> 0, "name" -> "MongoDB", "type" -> "database", "count" -> 1, "info" -> Document("x" -> 203, "y" -> 102))
//step 5 : insert in to db
collection.insertOne(doc).results();
在最后一步中results()
找不到该方法,但他们在文档中给出了它。