查找并更新 MongoDBRepository
我的任务是创建扩展 MongoDBRepository 的存储库,它允许我定义一种 findAndUpdate 方法。不允许 MongoOps 和模板方法。这就是我的 MongoDocument 的样子:
{"_id":"somedocumentId","attributetype1":{"class":"type1","value":"somevalue"},
"attributetype2":{"class":"type2","value":"somevalue"},
"attributetype3":{{"class":"type2","value":"somevalue"},{"class":"type2","value":"somevalue"},...,}
正如您所看到的,一些属性只是类值,而其他属性是列表。 这是我从消息传递子系统收到的更新请求:
{"documentId":"somedocumentIdValue","updatedata":{"class":"typeX","value":"somevalue"}
如果请求数据的类型只是一个类值(如 attributetype1 和 2),我需要更新它,如果更新数据的类型保存为列表,我需要附加它。
在这种情况下你有什么建议?
My task is to create repository extending MongoDBRepository that allows me to define kind of findAndUpdate method. MongoOps and template approach is not allowed. This is how my MongoDocument looks like:
{"_id":"somedocumentId","attributetype1":{"class":"type1","value":"somevalue"},
"attributetype2":{"class":"type2","value":"somevalue"},
"attributetype3":{{"class":"type2","value":"somevalue"},{"class":"type2","value":"somevalue"},...,}
As you can see some atributes are just class values, while others are lists.
Here is the update request I receive from my messaging subsystem:
{"documentId":"somedocumentIdValue","updatedata":{"class":"typeX","value":"somevalue"}
If type of request data is just a class value (like attributetype1 and 2) I need to update it, if update data is of type that is save as a list I need to append it.
What would you suggest in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要能够确定是否执行
$set
或$push
因此似乎有三种选择。更新消息传递子系统以包含操作员。
这可能不可行。
发出 findAndModify< /a> 与
$push
。如果它不是数组,这将失败:
<块引用>
无法将 $push/$pushAll 修饰符应用于非数组
因此用
$set
重新发出感觉像是黑客,但会起作用
执行一个返回文档的查询,并检查它,然后在更新查询中发出正确的运算符。
应用程序中需要更多代码
You need to be able to determine whether or not to do a
$set
or a$push
so there seems to be three choices.Update the messaging subsystem to include the operator.
This may not be feasible..
Issue a findAndModify with a
$push
.This will fail if its not an array:
So reissue with a
$set
Feels like a hack but will work
Do a query that returns the document and inspect it and then issue the correct operator in an update query.
More code required in the application