What is the difference between findOneAndUpdate and findOneAndReplace?

I recently installed the Java version of MongoDB Driver 3.1.1, and I wonder what the difference is between findOneAndUpdateand findOneAndReplace?

In what situation should I use each?

+4
source share
1 answer

findOneAndUpdateSearches for a document and updates only the records in this update document The remaining entries in the found document will remain.

findOneAndReplaceSearches for a document, deletes everything inside this document, and sets up records for this replacement document.

For example: You have a document{"name":"James", "age":"21"}

findOneAndUpdate {"age":"22"}, {"name":"James", "age":"22"}

findOneAndReplace {"age":"22"}, {"age":"22"} ( )

: findOneAndUpdate Documentation findOneAndReplace Documentation

+9

Source: https://habr.com/ru/post/1652513/


All Articles