문서 삭제
개요
이 가이드 에서는 코틀린 동기 (Kotlin Sync) 운전자 를 사용하여 삭제 작업 을 수행하여 MongoDB 컬렉션 에서 문서를 제거 하는 방법을 학습 수 있습니다.
삭제 작업은 MongoDB 컬렉션에서 하나 이상의 문서를 제거합니다. deleteOne()
또는 deleteMany()
메서드를 사용하여 삭제 작업을 수행할 수 있습니다.
샘플 데이터
이 가이드 의 예제에서는 Atlas 샘플 데이터 세트 의 sample_restaurants.restaurants
컬렉션 을 사용합니다. 무료 MongoDB Atlas cluster 를 생성하고 샘플 데이터 세트를 로드하는 방법을 학습 보려면 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 메서드에 대해 설명합니다.
메서드 | 설명 |
---|---|
| Specifies the kind of language collation to use when sorting
results. For more information, see Collation
in the MongoDB Server manual. |
| Specifies the index to use when matching documents.
For more information, see the hint statement
in the MongoDB Server manual. |
| Specifies the index as a string to use when matching documents.
For more information, see the hint statement
in the MongoDB Server manual. |
| 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. |
| 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
필드 값에 string "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
예외가 발생합니다. 서버 가 쓰기 (write) 작업을 승인하지 않으면 운전자 는 이러한 값을 결정할 수 없습니다.
API 문서
이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.