$addToSet implementation for array update on PATCH request

╄→гoц情女王★ 提交于 2020-01-15 09:46:06

问题


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

I am using Spring data rest with HATEOAS and Mongodb, and the current implementation replaces the old array with the new array, where the desired functionality is to perform a union of both arrays and have that array stored in the Mongodb document.

While it should be possible to do that with custom implementation, Spring won't generate a rest url for the implementation and hence would require a lot of boilerplate for a rather small requirement. All responses appreciated.


回答1:


Spring Data REST is build on top of domain objects and the repository abstraction. So letting it work with a store specific implementation is out of its scope. I am assuming your core goal is to apply a PATCH request to your domain instance and that $addToSet is just the means to achieve that.

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

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

Assuming c is the array your trying to manipulate, this would add foo and bar to it.

The other - more radical option is to actually deploy a manually implemented controller and hook it into the correct place in the URI space to manually interact with the repository.



来源:https://stackoverflow.com/questions/24814673/addtoset-implementation-for-array-update-on-patch-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!