List<WriteModel<Document>> / UpdateOneModel

Hanbyul·2023년 12월 21일

mongoDB

목록 보기
15/16

List<WriteModel< Document >>

List<WriteModel< Document >>는 MongoDB의 Java 드라이버에서 제공하는 일련의 쓰기 작업(Write operations)을 나타내는 객체들의 목록.
WriteModel은 MongoDB에서 수행할 수 있는 여러 종류의 쓰기 작업을 추상화한 인터페이스로, 쓰기 작업을 나타내는 여러 서브클래스가 있음.
예를 들어, InsertOneModel, UpdateOneModel, DeleteOneModel 등이 있음.

List<WriteModel< Document >>를 사용하면 여러 쓰기 작업을 단일 명령으로 MongoDB 서버에 배치할 수 있음.
이렇게 함으로써 네트워크 사용을 최적화하고, 여러 작업을 원자적으로(atomic) 처리할 수 있는 이점이 있음.
이 기능은 특히 대량의 데이터를 처리할 때 유용함.

UpdateOneModel

WriteModel< Document >의 서브클래스 중 하나인 UpdateOneModel는 하나의 문서에 대한 업데이트 작업을 나타냄.
UpdateOneModel 객체는 필터(어떤 문서를 업데이트할 것인지 지정)와 업데이트할 내용, 그리고 업데이트 옵션을 포함.
예를 들어, upsert 옵션을 true로 설정하면, 해당 필터에 일치하는 문서가 없으면 새 문서를 삽입하게 됨.

다음은 UpdateOneModel을 사용하는 간단한 예시입니다:

UpdateOneModel<Document> updateOneModel = new UpdateOneModel<>(
  Filters.eq("fieldToMatch", value), // 필터 조건
  new Document("$set", new Document("fieldToUpdate", newValue)), // 업데이트할 내용
  new UpdateOptions().upsert(true) // 업데이트 옵션
);

위의 코드 조각은 MongoDB에게 "fieldToMatch" 필드의 값이 value인 문서를 찾아 "fieldToUpdate" 필드를 newValue로 설정하도록 지시함. 만약 일치하는 문서가 없다면, 새 문서를 삽입.

List<WriteModel< Document >>에 여러 WriteModel 객체를 추가하고, 이 리스트를 MongoCollection.bulkWrite() 메서드에 전달하면, 모든 작업이 배치로 처리됨.

profile
공부공부

0개의 댓글