Docs Menu
Docs Home
/ / /
Kotlin Sync Driver

Write Data to MongoDB

On this page

  • Overview
  • Sample Application
  • Insert One
  • Insert Multiple
  • Update One
  • Update Multiple
  • Replace One
  • Delete One
  • Delete Multiple
  • Bulk Write

On this page, you can see copyable code examples of common methods that you can use to write data to MongoDB by using the Kotlin Sync driver.

Tip

To learn more about any of the methods shown on this page, see the link provided in each section.

To use an example from this page, copy the code example into the sample application or your own application. Be sure to replace all placeholders in the code examples, such as <connection string URI>, with the relevant values for your MongoDB deployment.

You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:

  1. Ensure you have the Kotlin Sync driver installed in your Maven or Gradle project.

  2. Copy the following code and paste it into a new .kt file.

  3. Copy a code example from this page and paste it on the specified lines in the file.

1import com.mongodb.ConnectionString
2import com.mongodb.MongoClientSettings
3import com.mongodb.client.model.*
4import com.mongodb.kotlin.client.*
5import org.bson.Document
6
7fun main() {
8 val uri = "<connection string URI>"
9
10 val settings = MongoClientSettings.builder()
11 .applyConnectionString(ConnectionString(uri))
12 .retryWrites(true)
13 .build()
14
15 // Create a new client and connect to the server
16 val mongoClient = MongoClient.create(settings)
17 val database = mongoClient.getDatabase("<database name>")
18 val collection = database.getCollection<Document>("<collection name>")
19
20 // Start example code here
21
22 // End example code here
23}

The following code shows how to insert a single document into a collection:

val result = collection.insertOne(Document("<field name>", "<value>"))
print(result.insertedId)

To learn more about the insertOne() method, see the Insert Documents guide.

The following code shows how to insert multiple documents into a collection:

val docList = listOf(
Document("<field name>", "<value>"),
Document("<field name>", "<value>")
)
val result = collection.insertMany(docList)
print(result.insertedIds)

To learn more about the insertMany() method, see the Insert Documents guide.

The following code shows how to update a single document in a collection by creating or editing a field:

val query = Filters.eq("<field to match>", "<value to match>")
val update = Updates.set("<field name>", "<value>")
val result = collection.updateOne(query, update)
print(result.modifiedCount)

To learn more about the updateOne() method, see the Update Documents guide.

The following code shows how to update multiple documents in a collection by creating or editing a field:

val query = Filters.eq("<field to match>", "<value to match>")
val update = Updates.set("<field name>", "<value>")
val result = collection.updateMany(query, update)
print(result.modifiedCount)

To learn more about the updateMany() method, see the Update Documents guide.

The following code shows how to replace a single document in a collection with a new document:

val query = Filters.eq("<field to match>", "<value to match>")
val replacement = Document("<new document field name>", "<new document value>")
val result = collection.replaceOne(query, replacement)
print(result.modifiedCount)

To learn more about the replaceOne() method, see the Replace Documents guide.

The following code shows how to delete a single document in a collection:

val query = Filters.eq("<field to match>", "<value to match>")
val result = collection.deleteOne(query)
print(result.deletedCount)

To learn more about the deleteOne() method, see the Delete Documents guide.

The following code shows how to delete multiple documents in a collection:

val query = Filters.eq("<field to match>", "<value to match>")
val result = collection.deleteMany(query)
print(result.deletedCount)

To learn more about the deleteMany() method, see the Delete Documents guide.

The following code shows how to perform multiple write operations in a single bulk operation:

val bulkOps = listOf(
InsertOneModel(Document("<field name>", "<value>")),
UpdateOneModel(
Filters.eq("<field to match>", "<value to match>"),
Updates.set("<field name>", "<value>")),
DeleteOneModel(Filters.eq("<field to match>", "<value to match>"))
)
val result = collection.bulkWrite(bulkOps)
print(result)

To learn more about the bulkWrite() method, see the Bulk Write guide.

Back

Stable API