$ addToSet实现,用于在PATCH请求上更新数组 [英] $addToSet implementation for array update on PATCH request

查看:112
本文介绍了$ addToSet实现,用于在PATCH请求上更新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PATCH请求期间更新数组值时,是否有办法告诉MongoRepository实现$ addToSet?

我正在使用带有HATEOAS和Mongodb的Spring数据其余部分,并且当前实现将旧数组替换为新数组,其中所需的功能是执行两个数组的合并并将该数组存储在Mongodb文档中. /p>

虽然可以通过自定义实现来做到这一点,但Spring不会为实现生成休息网址,因此,为一个相当小的需求而需要大量的样板.所有答复表示赞赏.

解决方案

Spring Data REST建立在域对象和存储库抽象之上.因此,使其与商店特定的实现一起工作超出了其范围.我假设您的核心目标是向您的域实例应用PATCH请求,而$addToSet只是实现此请求的方法.

从Spring Data REST的2.2 M1版本开始,我们在PATCH请求中支持 JSON补丁媒体类型 .因此,您可以将以下文档发送到服务器:

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

假设c是您要尝试操作的数组,这会向其中添加foobar.

另一个更激进的选择是实际部署手动实现的控制器,并将其挂接到URI空间中的正确位置,以便与存储库进行手动交互.

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.

解决方案

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.

这篇关于$ addToSet实现,用于在PATCH请求上更新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆