문서 메뉴
문서 홈
/ / /
Kotlin Sync 드라이버
/

문서 삭제

이 페이지의 내용

  • 개요
  • 샘플 데이터
  • 삭제 작업
  • 문서 하나 삭제
  • 여러 문서 삭제
  • 삭제 작업 사용자 지정
  • 반환 값
  • API 문서

이 가이드에서는 Kotlin Sync 드라이버를 사용하여 삭제 작업 을 수행하여 MongoDB 컬렉션에서 문서를 제거하는 방법에 대해 설명합니다.

삭제 작업은 MongoDB 컬렉션에서 하나 이상의 문서를 제거합니다. deleteOne() 또는 deleteMany() 메서드를 사용하여 삭제 작업을 수행할 수 있습니다.

이 가이드의 예제에서는 Atlas 샘플 데이터 세트sample_restaurants.restaurants 컬렉션을 사용합니다. 무료 MongoDB Atlas 클러스터를 생성하고 샘플 데이터 세트를 로드하는 방법을 알아보려면 Atlas 시작하기 가이드를 참조하세요.

이 컬렉션의 문서는 다음 Kotlin 데이터 클래스에 따라 모델링됩니다.

data class Restaurant(val name: String, val borough: String)

다음 방법을 사용하여 MongoDB에서 삭제 작업을 수행할 수 있습니다.

  • deleteOne()Atlas Search 기준과 일치 하는 첫 번째 문서를 삭제합니다.

  • deleteMany()Atlas Search 기준과 일치하는 모든 문서 를 삭제합니다.

각 삭제 메서드에는 제거하기 위해 선택할 문서를 결정하는 검색 기준을 지정하는 쿼리 필터 문서가 필요합니다. 쿼리 필터에 대해 자세히 알아보려면 쿼리 지정 가이드를 참조하세요.

다음 예제에서는 deleteOne() 메서드를 사용하여 name 필드 값이 "Happy Garden" 인 문서를 제거합니다.

val filter = eq(Restaurant::name.name, "Happy Garden")
val result = collection.deleteOne(filter)

다음 예제에서는 deleteMany() 메서드를 사용하여 borough 필드의 값이 "Brooklyn" 이고 name 필드의 값이 "Starbucks" 인 모든 문서를 제거합니다.

val filter = and(
eq(Restaurant::borough.name, "Brooklyn"),
eq(Restaurant::name.name, "Starbucks")
)
val result = collection.deleteMany(filter)

deleteOne()deleteMany() 메서드는 선택적으로 삭제 작업을 구성하는 데 사용할 수 있는 옵션을 나타내는 DeleteOptions 매개 변수를 허용합니다. 옵션을 지정하지 않으면 드라이버는 기본 설정으로 삭제 작업을 수행합니다.

다음 표에서는 DeleteOptions 인스턴스를 구성하는 데 사용할 수 있는 setter 메서드에 대해 설명합니다.

메서드
설명
collation()
Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.
hint()
Specifies the index to use when matching documents. For more information, see the hint statement in the MongoDB Server manual.
hintString()
Specifies the index as a string 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 delete command fields guide in the MongoDB Server manual for more information.

다음 코드는 옵션을 생성하고 comment() 메서드를 사용하여 삭제 작업에 주석을 추가합니다. 그런 다음 이 예제에서는 deleteMany() 메서드를 사용하여 restaurants 컬렉션에서 name 필드 값에 "Red" 문자열이 포함된 모든 문서를 삭제합니다.

val opts = DeleteOptions().comment("sample comment")
val filter = regex(Restaurant::name.name, "Red")
val result = collection.deleteOne(filter, opts)

앞의 예에서 deleteMany() 메서드 대신 deleteOne() 메서드를 사용하는 경우 드라이버는 쿼리 필터와 일치하는 첫 번째 문서만 삭제합니다.

deleteOne()deleteMany() 메서드는 각각 DeleteResult 인스턴스를 반환합니다. DeleteResult 인스턴스에서 다음 정보에 액세스할 수 있습니다.

  • deletedCount삭제된 문서 수를 나타냅니다.

  • wasAcknowledged()서버가 결과를 확인하면 true 를 반환합니다.

쿼리 필터가 어떤 문서와도 일치하지 않는 경우 드라이버는 어떤 문서도 삭제하지 않으며 deletedCount 값은 0 입니다.

참고

wasAcknowledged() 메서드가 false 을 반환하는 경우 deletedCount 속성에 액세스하려고 하면 InvalidOperation 예외가 발생합니다. 서버가 쓰기 작업을 승인하지 않으면 드라이버는 이러한 값을 결정할 수 없습니다.

이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.

돌아가기

문서 교체