Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversKotlin Coroutine

Update a Document

You can update a single document using the updateOne() method on a MongoCollection object. The method accepts a filter that matches the document you want to update and an update statement that instructs the driver how to change the matching document. The updateOne() method only updates the first document that matches the filter.

To perform an update with the updateOne() method, you must pass a query filter and an update document. The query filter specifies the criteria for which document to perform the update on and the update document provides instructions on what changes to make to it.

You can optionally pass an instance of UpdateOptions to the updateOne() method in order to specify the method's behavior. For example, if you set the upsert field of the UpdateOptions object to true, the operation inserts a new document from the fields in both the query and update document if no documents match the query filter. See the link to the UpdateOptions API documentation at the bottom of this page for more information.

Upon successful execution, the updateOne() method returns an instance of UpdateResult. You can retrieve information such as the number of documents modified by calling the getModifiedCount() method, or the value of the _id field by calling the getUpsertedId() method if you specified upsert(true) in an UpdateOptions instance.

If your update operation fails, the driver raises an exception. For example, if you try to set a value for the immutable field _id in your update document, the method throws a MongoWriteException with the message:

Performing an update on the path '_id' would modify the immutable field '_id'

If your update document contains a change that violates unique index rules, the method throws a MongoWriteException with an error message that should look something like this:

E11000 duplicate key error collection: ...

For more information on the types of exceptions raised under specific conditions, see the updateOne() API documentation linked at the bottom of this page.

In this example, we use a Filter builder to query the collection for a movie with the title "Cool Runnings 2".

Next, we perform the following updates to the first match for our query in the movies collection of the sample_mflix database:

  1. Set the value of runtime to 99

  2. Add Sports to the array of genres only if it does not already exist

  3. Set the value of lastUpdated to the current time

We use the Updates builder, a factory class that contains static helper methods, to construct the update document. While you can pass an update document instead of using the builder, the builder provides type checking and simplified syntax. See the guide on the Updates builder for more information.

Note

This example connects to an instance of MongoDB using a connection URI. To learn more about connecting to your MongoDB instance, see the connection guide.

import com.mongodb.MongoException
import com.mongodb.client.model.Filters
import com.mongodb.client.model.UpdateOptions
import com.mongodb.client.model.Updates
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.runBlocking
import java.time.LocalDateTime
data class Movie(
val title: String,
val runtime: Int,
val genres: List<String>,
val lastUpdated: LocalDateTime
)
fun main() = runBlocking {
// Replace the uri string with your MongoDB deployment's connection string
val uri = "<connection string uri>"
val mongoClient = MongoClient.create(uri)
val database = mongoClient.getDatabase("sample_mflix")
val collection = database.getCollection<Movie>("movies")
val query = Filters.eq(Movie::title.name, "Cool Runnings 2")
val updates = Updates.combine(
Updates.set(Movie::runtime.name, 99),
Updates.addToSet(Movie::genres.name, "Sports"),
Updates.currentDate(Movie::lastUpdated.name)
)
val options = UpdateOptions().upsert(true)
try {
val result = collection.updateOne(query, updates, options)
println("Modified document count: " + result.modifiedCount)
println("Upserted id: " + result.upsertedId) // only contains a non-null value when an upsert is performed
} catch (e: MongoException) {
System.err.println("Unable to update due to an error: $e")
}
mongoClient.close()
}

After you run the example, you should see output that looks something like this:

Modified document count: 1
Upserted id: null

Or if the example resulted in an upsert:

Modified document count: 0
Upserted id: BsonObjectId{value=...}

If you query the updated document, it should look something like this:

Movie(title=Cool Runnings 2, runtime=99, genres=[ ... Sports], lastUpdated= ... )

For additional information on the classes and methods mentioned on this page, see the following API Documentation:

  • UpdateOne

  • UpdateOptions

  • combine()

  • set()

  • addToSet()

  • currentDate()

  • UpdateResult

←  Update & Replace OperationsUpdate Multiple Documents →