java - 如何使用存储库模式更新 mongodb 中的嵌入文档

标签 java mongodb spring-boot

如何使用 Spring Data Repository 更新 MongoDB 中的嵌入文档。

[{
    "_id": 1,
    "hotel_name": "test_hotel",
    "reviews": [

      {
        "id": 1,
        "rating": "4star",
        "username": "test_user"
      },
      {
        "id": 2,
        "rating": "4star",
        "username": "test_user"
      },
      {
        "id": 3,
        "rating": "4star",
        "username": "test_user"
      }
    ]

  },
  {
    "_id": 2,
    "hotel_name": "test_hotel2",
    "reviews": [

      {
        "id": 1,
        "rating": "4star",
        "username": "test_user"
      },
      {
        "id": 2,
        "rating": "4star",
        "username": "test_user"
      },
      {
        "id": 3,
        "rating": "4star",
        "username": "test_user"
      }
    ]

  }
]

例如,在上面的 json 中,我想更新第二家酒店,评论 ID 2。 您能帮助我如何实现此要求的存储库吗?谢谢。

最佳答案

使用Spring Stack,您可以使用SpringData来完成此操作,您只需要实现一个扩展MongoRepository的接口(interface)并使用Update类进行更新即可。

我找到了一个详细的快速入门示例: https://www.baeldung.com/spring-data-mongodb-tutorial

关于java - 如何使用存储库模式更新 mongodb 中的嵌入文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57367534/

相关文章:

mongodb - Mongo LiveCode MergJSON 无法解码文档

java - 在 spring boot 中加载 war 文件之外的属性文件

java - Spring Boot从自定义类访问dao服务

java - Solrcloud性能问题

java - 如何将科学运算符 E 与变量一起使用?

python - Pymongo-值错误 : NaTType does not support utcoffset when using insert_many

java - 将 HttpServletResponse 转换为 json

java - 改进基于上下文的搜索

java - 如何在 Android 中获取 LinkedIn 联系人或好友列表?

c++ - 如何在 Ubuntu 中安装和使用 mongodb c++ 驱动程序?