Write Data to MongoDB
On this page
Overview
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.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have the Kotlin Sync driver installed in your Maven or Gradle project.
Copy the following code and paste it into a new
.kt
file.Copy a code example from this page and paste it on the specified lines in the file.
1 import com.mongodb.ConnectionString 2 import com.mongodb.MongoClientSettings 3 import com.mongodb.client.model.* 4 import com.mongodb.kotlin.client.* 5 import org.bson.Document 6 7 fun 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 }
Insert One
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.
Insert Multiple
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.
Update One
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.
Update Multiple
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.
Replace One
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.
Delete One
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.
Delete Multiple
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.
Bulk Write
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.