将数据写入 MongoDB
Overview
在此页面上,您可以查看可复制的代码示例,这些示例展示了使用Scala驾驶员将数据写入MongoDB的常用方法。
提示
要了解有关此页面上显示的任何方法的更多信息,请参阅每个部分中提供的链接。
要使用本页中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 请务必将代码示例中的所有占位符(例如 <connection string URI>
)替换为 MongoDB 部署的相关值。
示例应用程序
您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:
确保您已在 Maven 或 sbt项目中安装Scala驾驶员。
复制以下代码并将其粘贴到新的
.scala
文件中。从此页面复制代码示例,并将其粘贴到文件中的指定行。
1 import org.mongodb.scala._ 2 import org.mongodb.scala.bson.Document 3 import org.mongodb.scala.model._ 4 import org.mongodb.scala.model.Filters._ 5 import org.mongodb.scala.result._ 6 import org.mongodb.scala.model.Updates._ 7 8 object SampleWriteApp { 9 10 def main(args: Array[String]): Unit = { 11 val mongoClient = MongoClient("<connection string URI>") 12 val database: MongoDatabase = mongoClient.getDatabase("<database name>") 13 val collection: MongoCollection[Document] = database.getCollection("<collection name>") 14 15 // Start example code here 16 17 // End example code here 18 19 // Wait for the operations to complete before closing client 20 // Note: This example uses Thread.sleep() for brevity and does not guarantee all 21 // operations will be completed in time 22 Thread.sleep(1000) 23 mongoClient.close() 24 } 25 }
insertOne
以下代码演示如何将单个文档插入集合:
val doc: Document = Document("<field name>" -> "<value>") val observable: Observable[InsertOneResult] = collection.insertOne(doc) observable.subscribe(new Observer[InsertOneResult] { override def onNext(result: InsertOneResult): Unit = println(result) override def onError(e: Throwable): Unit = println("Failed: " + e.getMessage) override def onComplete(): Unit = println("Completed") })
要学习;了解有关insertOne()
方法的更多信息,请参阅插入文档指南。
插入多个
以下代码演示了如何将多个文档插入到集合中:
val docs: Seq[Document] = Seq( Document("<field name>" -> "<value>"), Document("<field name>" -> "<value>") ) val observable: Observable[InsertManyResult] = collection.insertMany(docs) observable.subscribe(new Observer[InsertManyResult] { override def onNext(result: InsertManyResult): Unit = println(result) override def onError(e: Throwable): Unit = println("Failed: " + e.getMessage) override def onComplete(): Unit = println("Completed") })
要了解有关insertMany()
方法的更多信息,请参阅“插入文档”指南。
updateOne
以下代码演示如何通过创建或编辑字段来更新集合中的单个文档:
val filter = equal("<field to match>", "<value to match>") val update = set("<field name>", "<value>") val observable: Observable[UpdateResult] = collection.updateOne(filter, update) observable.subscribe(new Observer[UpdateResult] { override def onNext(result: UpdateResult): Unit = println(result) override def onError(e: Throwable): Unit = println("Failed: " + e.getMessage) override def onComplete(): Unit = println("Completed") })
要了解有关updateOne()
方法的更多信息,请参阅更新文档指南。
更新多个
以下代码演示如何通过创建或编辑字段来更新集合中的多个文档:
val filter = equal("<field to match>", "value to match") val update = set("<field name>", "<value>") val observable: Observable[UpdateResult] = collection.updateMany(filter, update) observable.subscribe(new Observer[UpdateResult] { override def onNext(result: UpdateResult): Unit = println(result) override def onError(e: Throwable): Unit = println("Failed: " + e.getMessage) override def onComplete(): Unit = println("Completed") })
要了解有关updateMany()
方法的更多信息,请参阅更新文档指南。
replaceOne
以下代码演示如何用新文档替换集合中的单个文档:
val filter = equal("<field to match>", "<value to match>") val replacementDoc: Document = Document("<field name>" -> "<value>") val observable: Observable[UpdateResult] = collection.replaceOne(filter, replacementDoc) observable.subscribe(new Observer[UpdateResult] { override def onNext(result: UpdateResult): Unit = println(result) override def onError(e: Throwable): Unit = println("Failed: " + e.getMessage) override def onComplete(): Unit = println("Completed") })
要了解有关replaceOne()
方法的更多信息,请参阅替换文档指南。
deleteOne
以下代码演示如何删除集合中的单个文档:
val filter = equal("<field to match>", "<value to match>") val observable: Observable[DeleteResult] = collection.deleteOne(filter) observable.subscribe(new Observer[DeleteResult] { override def onNext(result: DeleteResult): Unit = println(result) override def onError(e: Throwable): Unit = println("Failed: " + e.getMessage) override def onComplete(): Unit = println("Completed") })
要了解有关deleteOne()
方法的更多信息,请参阅“删除文档”指南。
删除多个
以下代码演示如何删除集合中的多个文档:
val filter = equal("<field to match>", "<value to match>") val observable: Observable[DeleteResult] = collection.deleteMany(filter) observable.subscribe(new Observer[DeleteResult] { override def onNext(result: DeleteResult): Unit = println(result) override def onError(e: Throwable): Unit = println("Failed: " + e.getMessage) override def onComplete(): Unit = println("Completed") })
要了解有关deleteMany()
方法的更多信息,请参阅“删除文档”指南。
批量写入
以下代码展示了如何在单个批量操作中执行多个写入操作:
val operations = Seq( InsertOneModel(Document("<field name>" -> "<value>")), UpdateManyModel(equal("<field to match>", "<value to match>"), set("<field name>", "<value>")), DeleteOneModel(equal("<field to match>", "<value to match>")) ) val observable: Observable[BulkWriteResult] = collection.bulkWrite(operations) observable.subscribe(new Observer[BulkWriteResult] { override def onNext(result: BulkWriteResult): Unit = println(result) override def onError(e: Throwable): Unit = println("Failed: " + e.getMessage) override def onComplete(): Unit = println("Completed") })