When replacing a JSON object embedded in a Couchbase document, is it mandatory to pull the document down to the client first or is there a way to call the bucket’s replace method passing the ID of the document to replace?
// New JSON object of prices
JsonObject newPrices = JsonObject.empty();
// Get document
JsonDocument product = bucket.get(row.id());
JsonDocument updated = bucket.replace(product);
// Can I do something like this instead?
JsonDocument updated = bucket.replace("prices", newPrices, row.id());
replace method replaces the whole document. What you are trying to do is a replace of just a sub-section of the JSON content.
So you must have the full JSON document. And if you don’t retrieve it from the database, you also want to be sure that other parts have not been edited elsewhere, or that such edits can be overwritten…
Note that since you seem to be using views, you can use the shortcut
row.document() to load the full document instead of
Bucket.get(id) (it also does a
bucket.get under the cover).
In the future, this use case may be supported through N1QL (although data modification requests are not yet targeted by the SDK in the upcoming developer preview).
Fair point. Thanks Simon.