在我的项目中,我使用 SpringBoot 1.3.2 和 org.springframework.data.mongodb.core.query.*
我正在尝试从嵌套对象数组中删除元素,在我的主对象中,我的数组如下所示:
"sections" : [
{
"sectionId" : "56cc3c908f5e6c56e677bd2e",
"name" : "Wellcome"
},
{
"sectionId" : "56cc3cd28f5e6c56e677bd2f",
"name" : "Hello my friends"
}
]
Run Code Online (Sandbox Code Playgroud)
使用Spring我想删除sectionId为56cc3c908f5e6c56e677bd2e的记录
这就是我尝试这样做的方式:
Query query = Query.query(Criteria
.where("sections")
.elemMatch(
Criteria.where("sectionId").is("56cc3c908f5e6c56e677bd2e")
)
);
Update update = new Update().unset("sections.sectionId");
mongoTemplate.updateMulti(query, update, Offer.class);
Run Code Online (Sandbox Code Playgroud)
查询正在找到正确的元素,但更新有问题,我不知道什么所以删除不起作用。
有谁可以帮助我解决这个问题吗?
因为无论如何我都需要练习,所以这里是你想要的猜测。
Query query = Query.query(Criteria
.where("sections")
.elemMatch(
Criteria.where("sectionId").is("56cc3c908f5e6c56e677bd2e")
)
);
Update update =
new Update().pull("sections",
new BasicDBObject("sectionId", "56cc3c908f5e6c56e677bd2e"));
mongoTemplate.updateMulti(query, update, Offer.class);
Run Code Online (Sandbox Code Playgroud)
导致
"sections" : [
{
"sectionId" : "56cc3cd28f5e6c56e677bd2f",
"name" : "Hello my friends"
}
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4349 次 |
| 最近记录: |