$ addToSet to update array on PATCH request

Is there a way to tell MongoRepository to implement $ addToSet when updating an array value during a PATCH request?

I use Spring the rest of the data with HATEOAS and Mongodb, and the current implementation replaces the old array with a new array, where the desired functionality is to combine both arrays and have this array stored in a Mongodb document.

While this should be possible with a custom implementation, Spring will not generate a rest URL for the implementation, and therefore will require many templates for fairly small requirements. All reviews are rated.

+1
source share
1 answer

Spring Data REST is created on top of domain objects and repository abstractions. Thus, letting him work with the store’s implementation goes beyond him. I assume that your main goal is to apply the PATCH request to your domain instance and that $addToSet is just a means to achieve this.

Starting with version 2.2 of M1 Spring Data REST, we support JSON Patch media type on PATCH request. Thus, you can send the following document to the server:

 [{ "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] }] 

Assuming c is the array you are trying to manipulate, this will add foo and bar to it.

Another - more radical option is to actually deploy the controller with manual input and bind it to the right place in the URI space for manual interaction with the repository.

+2
source

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


All Articles