Spring/MongoDB问题:MongoOperation.updateMulti仅更新一个字段。

huangapple go评论75阅读模式
英文:

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
);

huangapple
  • 本文由 发表于 2020年10月16日 23:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64391666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定