Skip to content

Commit 814bbcf

Browse files
committed
docs: add comprehensive documentation for partial updates on @document entities (#595)
- Document the existing partial update functionality for JSON documents - Add examples for single entity updates using Query by Example - Include bulk update operations and ExampleMatcher usage - Explain how JSON.SET with path specifications enables atomic field updates - Clarify that this feature is already implemented and tested
1 parent cd4ff3a commit 814bbcf

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

docs/content/modules/ROOT/pages/json-repositories.adoc

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,112 @@ public interface CompanyRepository extends RedisDocumentRepository<Company, Stri
251251
}
252252
----
253253

254+
== Partial Updates
255+
256+
Redis OM Spring supports partial updates on JSON documents using the Query by Example (QBE) pattern. This feature allows you to update specific fields without affecting other parts of the document, leveraging RedisJSON's JSON.SET command with path specifications.
257+
258+
=== Update Single Entity
259+
260+
Update specific fields of an existing entity:
261+
262+
[source,java]
263+
----
264+
@Service
265+
public class CompanyService {
266+
@Autowired
267+
private CompanyRepository repository;
268+
269+
public void partialUpdate() {
270+
// Create an update probe with only the fields to update
271+
Company updateProbe = new Company();
272+
updateProbe.setId("company:1"); // ID is required
273+
updateProbe.setEmail("[email protected]"); // Update only email
274+
275+
// Execute partial update - only non-null fields are updated
276+
Company updated = repository.update(Example.of(updateProbe));
277+
278+
// The returned entity has all fields, with only email changed
279+
System.out.println(updated.getName()); // Original name preserved
280+
System.out.println(updated.getEmail()); // New email value
281+
}
282+
}
283+
----
284+
285+
=== Update with ExampleMatcher
286+
287+
Use ExampleMatcher for fine-grained control:
288+
289+
[source,java]
290+
----
291+
public void updateWithMatcher() {
292+
Company updateProbe = new Company();
293+
updateProbe.setId("company:1");
294+
updateProbe.setName("Redis Labs");
295+
updateProbe.setEmail("[email protected]");
296+
297+
// Ignore specific fields during update
298+
ExampleMatcher matcher = ExampleMatcher.matching()
299+
.withIgnorePaths("email"); // Don't update email even if set
300+
301+
repository.update(Example.of(updateProbe, matcher));
302+
// Only name is updated, email remains unchanged
303+
}
304+
----
305+
306+
=== Bulk Partial Updates
307+
308+
Update multiple entities efficiently:
309+
310+
[source,java]
311+
----
312+
public void bulkPartialUpdate() {
313+
List<Example<Company>> updates = new ArrayList<>();
314+
315+
// Prepare multiple updates
316+
Company update1 = new Company();
317+
update1.setId("company:1");
318+
update1.setPubliclyListed(true);
319+
updates.add(Example.of(update1));
320+
321+
Company update2 = new Company();
322+
update2.setId("company:2");
323+
update2.setYearFounded(2012);
324+
updates.add(Example.of(update2));
325+
326+
// Execute all updates in a single pipelined operation
327+
repository.updateAll(updates);
328+
}
329+
----
330+
331+
=== Update Nested Objects
332+
333+
Partial updates work with nested structures:
334+
335+
[source,java]
336+
----
337+
public void updateNestedObject() {
338+
Company updateProbe = new Company();
339+
updateProbe.setId("company:1");
340+
341+
// Update nested address object
342+
Address newAddress = new Address();
343+
newAddress.setCity("Mountain View");
344+
newAddress.setCountry("USA");
345+
updateProbe.setAddress(newAddress);
346+
347+
repository.update(Example.of(updateProbe));
348+
// Only the address field is updated
349+
}
350+
----
351+
352+
=== Important Notes
353+
354+
* **ID Required**: The update probe must have a non-null ID
355+
* **Non-null Fields**: Only non-null fields in the probe are updated
356+
* **Atomic Updates**: Each field is updated atomically using JSON.SET
357+
* **Existing Documents**: Updates only affect existing documents (uses XX flag)
358+
* **Performance**: Bulk updates use pipelining for better performance
359+
254360
== Example Usage
255361

256362
Here's a complete example showing repository usage:

0 commit comments

Comments
 (0)