查找并更新 MongoDBRepository

发布于 2025-01-03 23:42:56 字数 664 浏览 4 评论 0原文

我的任务是创建扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染柒℉ 2025-01-10 23:42:56

您需要能够确定是否执行 $set$push 因此似乎有三种选择。

  1. 更新消息传递子系统以包含操作员。

    这可能不可行。

  2. 发出 findAndModify< /a> 与 $push

    如果它不是数组,这将失败:

    <块引用>

    无法将 $push/$pushAll 修饰符应用于非数组

    因此用 $set 重新发出

    感觉像是黑客,但会起作用

  3. 执行一个返回文档的查询,并检查它,然后在更新查询中发出正确的运算符。

    应用程序中需要更多代码

You need to be able to determine whether or not to do a $set or a $push so there seems to be three choices.

  1. Update the messaging subsystem to include the operator.

    This may not be feasible..

  2. Issue a findAndModify with a $push.

    This will fail if its not an array:

    Cannot apply $push/$pushAll modifier to non-array

    So reissue with a $set

    Feels like a hack but will work

  3. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文