如何使用 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/