英文:
Spring/MongoDB Probleme with MongoOperation.updateMulti that update only one field
问题
在我的活动文档中,我想要更新一个动作状态的集合,这些动作的标识位于一个列表中:
{
"_id" : "...",
"actions" : [
{
"_id" : 1,
"status" : "todo"
},
{
"_id" : 2,
"status" : "in progress"
},
{
"_id" : 3,
"status" : "done"
},
{
"_id" : 4,
"status" : "done"
},
{
"_id" : 5,
"status" : "todo"
}
]
}
我尝试使用MongoOperation.updateMulti
编写代码,但它只更新了一个状态:
mongoOperation.updateMulti(
new Query().addCriteria(
Criteria.where("_id").is(activityId).and("actions._id").in(actionsIds)),
new Update().set("actions.$.status", newStatut),
ActivityModel.class
);
我不知道问题出在哪里。是我的查询有问题吗?还是更新有问题?
英文:
In my Activity document I want to update a collection of actions status which ids are in a list :
{
"_id" : "...",
"actions" : [
{
"_id" : 1,
"status" : "todo"
},
{
"_id" : 2,
"status" : "in progress"
},
{
"_id" : 3,
"status" : "done"
},
{
"_id" : 4,
"status" : "done"
},
{
"_id" : 5,
"status" : "todo"
}
]
}
I tried to write a code using MongoOperation.updateMulti but it updates only one status at all :
mongoOperation.updateMulti(
new Query().addCriteria(
Criteria.where("_id").is(activityId).and("actionsActivite._id").in(actionsIds)),
new Update().set("actionsActivite.$.status", newStatut),
ActivityModel.class
);
I don't know where the problem is. Is my Query wrong ? My Update ?
答案1
得分: 0
我终于找到了解决方案。我只需要在更新中添加$[]运算符,就像这样:
mongoOperation.updateMulti(
new Query().addCriteria(
Criteria.where("id").is(activityId).and("actionsActivity.id").in(actionsIds)),
new Update().set("actionsActivity.$[].status", newStatus),
ActivityModel.class
);
英文:
I finally found the solution. I just add $[] operator in the update like this :
mongoOperation.updateMulti(
new Query().addCriteria(
Criteria.where("_id").is(activityId).and("actionsActivite._id").in(actionsIds)),
new Update().set("actionsActivite.$[].status", newStatut),
ActivityModel.class
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论