Docs Menu
Docs Home
/ / /
Kotlin Sync ドライバー
/

Update Documents

項目一覧

  • Overview
  • サンプル データ
  • アップデート操作
  • 1 つのドキュメントの更新
  • 多くのドキュメントの更新
  • 更新操作をカスタマイズする
  • 戻り値
  • 詳細情報
  • API ドキュメント

このガイドでは、 Kotlin Sync ドライバーを使用して MongoDB コレクション内のドキュメントを更新する方法を学習できます。これには updateOne()updateMany()メソッドが使用されます。

このガイドの例では、 Atlas サンプル データセットsample_restaurants.restaurantsコレクションを使用します。 MongoDB Atlas クラスターを無料で作成して、サンプル データセットをロードする方法については、 「 Atlas を使い始める 」ガイドを参照してください。

このコレクション内のドキュメントは、次の Kotlin データ クラスによってモデル化されます。

data class Restaurant(
val name: String,
val borough: String,
val cuisine: String,
val address: Document
)

次の方法を使用して、MongoDB 内のドキュメントを更新できます。

  • updateOne()は、検索条件に一致する最初のドキュメントを更新します。

  • updateMany()は、検索条件に一致するすべてのドキュメントを更新します

各更新方法には次のパラメーターが必要です。

  • クエリフィルター。アップデートするドキュメントに一致する。 クエリフィルターの詳細については、「 クエリの指定」ガイドを参照してください。

  • 更新ドキュメント。更新演算子または実行する更新の種類と、更新されるフィールドと値を指定します。 更新演算子とその使用方法のリストについては、 MongoDB Serverマニュアルの「 フィールド更新演算子 」のガイド ページ を参照してください。

次の例では、 updateOne()メソッドを使用して、ドキュメントのnameの値を"Happy Garden"から"Mountain House"に更新します。

val filter = eq(Restaurant::name.name, "Happy Garden")
val update = set(Restaurant::name.name, "Mountain House")
val result = collection.updateOne(filter, update)

次の例では、 updateMany()メソッドを使用して、 nameの値が"Starbucks"であるすべてのドキュメントを更新します。 アップデートにより、 addressフィールドの名前がlocationに変更されます。

val filter = eq(Restaurant::name.name, "Starbucks")
val update = rename(Restaurant::address.name, "location")
val result = collection.updateMany(filter, update)

updateOne()メソッドとupdateMany()メソッドは、更新操作を構成するためのオプションを設定する パラメーターをオプションで受け入れます。 オプションを指定しない場合、ドライバーはデフォルト設定で更新操作を実行します。

次の表では、 UpdateOptionsインスタンスを構成するために使用できる setter メソッドについて説明します。

プロパティ
説明
upsert()
Specifies whether the update operation performs an upsert operation if no documents match the query filter. For more information, see the upsert statement in the MongoDB Server manual.
Defaults to false
bypassDocumentValidation()
Specifies whether the update operation bypasses document validation. This lets you update documents that don't meet the schema validation requirements, if any exist. For more information about schema validation, see Schema Validation in the MongoDB Server manual.
Defaults to false.
collation()
Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.
arrayFilters()
Provides a list of filters that you specify to select which array elements the update applies to.
hint()
Sets the index to use when matching documents. For more information, see the hint statement in the MongoDB Server manual.
let()
Provides a map of parameter names and values to set top-level variables for the operation. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual.
comment()
Sets a comment to attach to the operation. For more information, see the update command fields guide in the MongoDB Server manual for more information.

次のコードでは、 updateOne()メソッドを使用して、 nameフィールドの値が"Sunrise Pizzeria"であるドキュメントを一致させます。 次に、最初に一致するドキュメントのboroughの値を"Queens"に、 cuisineの値を"Italian"に設定します。

upsertオプションがtrueに設定されているため、クエリフィルターが既存のドキュメントと一致しない場合、ドライバーはアップデート ドキュメントで指定されたフィールドと値を含む新しいドキュメントを挿入します。

val opts = UpdateOptions().upsert(true)
val filter = eq(Restaurant::name.name, "Sunrise Pizzeria")
val update = combine(
set(Restaurant::borough.name, "Queens"),
set(Restaurant::cuisine.name, "Italian")
)
collection.updateOne(filter, update, opts)

updateOne()メソッドとupdateMany()メソッドはそれぞれUpdateResultオブジェクトを返します。 UpdateResultインスタンスからの情報にアクセスするには、次のメソッドを使用できます。

プロパティ
説明
getMatchedCount()
Returns the number of documents that matched the query filter, regardless of how many updates were performed.
getModifiedCount()
Returns the number of documents modified by the update operation. If an updated document is identical to the original, it is not included in this count.
wasAcknowledged()
Returns true if the server acknowledged the result.
getUpsertedId()
Returns the _id value of the document that was upserted in the database, if the driver performed an upsert.

注意

wasAcknowledged()メソッドがfalseを返す場合、 UpdateResultインスタンスから他の情報にアクセスしようとすると、 InvalidOperation例外が発生します。 サーバーが書込み (write) 操作を確認しない場合、ドライバーはこれらの値を決定できません。

Kotlin Sync ドライバーを使用してドキュメントを更新する方法を示す実行可能なコード例については、「 MongoDB へのデータの書き込み 」を参照してください。

このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。

戻る

ドキュメントの挿入