📌 UpdateResult 와 WriteResult 란?
UpdateResult와 WriteResult 는 모두 데이터베이스에 수행된 작업의 결과를 나타내는 클래스입니다.
하지만 이 둘 클래스는 조금 다르게 사용됩니다.
📌 UpdateResult 와 WriteResult 의 차이
UpdateResult | WriteResult |
- MongoDB Java Driver 버전 3.0 이후 부터 제공되는 클래스다. - 하나 이상의 문서를 update(수정)에서 사용되며, 작업의 결과를 구체적으로 나타낸다. - Update 쿼리와 일치하는 문서 수, 수정된 문서 수 및 기타 관련 정보를 제공한다. |
- insert, update, delete 등 단일 쓰기 작업의 결과를 나타낸다 - 작업으로 인해 영향을 받은 문서 수, 발생할 수 있는 오류, 기타 메타데이터 등의 정보를 포함한다. |
📌 Conclusion
WriteResult 인 경우엔 insert, update, delete 등 광범위하게 사용 가능하다.
반면에 UpdateResult는 특정적으로 Update 기능을 사용할 때 사용되며 Java Driver version 이 3.0 이후부터 사용 가능하다. 참고로 UpdateResult 말고도 DeleteResult도 존재한다.
<참고자료>
ChatGPT
In MongoDB's Java driver and Spring Data MongoDB's MongoTemplate, both UpdateResult and WriteResult are classes that represent the outcome of operations performed on the database. However, they are used in different contexts and represent different types of operations.
- WriteResult:
- WriteResult is a class provided by the MongoDB Java driver.
- It represents the result of a single write operation, such as an insert, update, remove, etc.
- It contains information such as the number of documents affected by the operation, any errors that may have occurred, and other metadata.
- WriteResult is used for operations that modify data in the database.
- UpdateResult:
- UpdateResult is a class provided by the MongoDB Java driver starting from version 3.0.
- It specifically represents the result of an update operation, such as an update query that modifies one or more documents in a collection.
- UpdateResult provides information such as the number of documents matched by the update query, the number of documents modified (if any), and other relevant information.
- It is specifically designed to handle the outcomes of update operations.
In Spring Data MongoDB's MongoTemplate, when you perform operations like updateFirst, updateMulti, or update, you typically receive an UpdateResult object as the result. This object allows you to inspect the outcome of the update operation, such as how many documents matched the query and how many were actually modified.
On the other hand, when you perform operations like insert, save, or remove, you usually get a WriteResult object, which represents the outcome of these write operations.
In summary, while both UpdateResult and WriteResult represent outcomes of database operations in MongoDB, UpdateResult is specifically tailored for update operations, while WriteResult is more general and applicable to various types of write operations.
'Spring > Skill' 카테고리의 다른 글
CORS와 Preflight Request (0) | 2024.03.25 |
---|---|
[MyBatis] upsert (insert + update) mapper 작성법 (0) | 2024.03.18 |
[spring] OAuth2 네이버 인증하기 (0) | 2024.02.12 |
[Javascript] active 값 활성화하기 (0) | 2024.02.12 |
[spring] OAuth2 구글 인증하기 (0) | 2024.02.07 |