Spring boot 使用spring boot mongotemplate更新二级嵌套数组元素时出现问题

Spring boot 使用spring boot mongotemplate更新二级嵌套数组元素时出现问题,spring-boot,spring-data-mongodb,mongotemplate,Spring Boot,Spring Data Mongodb,Mongotemplate,问题文件 { "_id": ObjetId("5b91ce1d001902722e79539b") ... "questionDetail": { ... "answers": [ { "srno": 10, ... "userMatchPairAnswerCounts": [

问题文件

{
  "_id": ObjetId("5b91ce1d001902722e79539b")
  ...
  "questionDetail": {
   ...
    "answers": [
      {
        "srno": 10,
        ...
        "userMatchPairAnswerCounts": [
          {
            "anssrno": 8,
            "anscnt": 0
          },
          {
            "anssrno": 9,
            "anscnt": 0
          }
        ]
      },
      {
        "srno": 20,
        ...
        "userMatchPairAnswerCounts": [
          {
            "anssrno": 14,
            "anscnt": 0
          },
          {
            "anssrno": 15,
            "anscnt": 0
          },
          {
            "anssrno": 16,
            "anscnt": 0
          }
        ]
      }
      ...
}
Java类

public class Question {
   private QuestionDetail  questionDetail;
   ...
}

public class QuestionDetail {
  private List<Answer> answers = new ArrayList<>();
  ...
}


public class Answer {
  private int srno;
  private List<UserMatchPairAnswerCount>  userMatchPairAnswerCounts; 
  ...  
}

public class UserMatchPairAnswerCount {
  private int anssrno;
  private long anscnt;
  ...
}
上面修改了“anssrno”:8(索引1-与所选“答案”索引相同)

上面修改了“anssrno”:15(索引2-与所选“答案”索引相同)

看起来像filterArray标准

"answer.userMatchPairAnswerCounts").elemMatch(Criteria.where("anssrno").is(16))));
没有任何影响,而是始终根据所选答案的索引更新/插入(如果所需索引中没有文档)

我做错了什么?请引导

更新:

直接命令行工作正常:

db.questions.update({
  "_id": ObjectId("5b91ce1d001902722e79539b"),
  "questionDetail.answers.srno": 10
},
{
  "$inc": {
    "questionDetail.answers.$[answer].userMatchPairAnswerCounts.$[userMtc].anscnt": 1
  }
},
{
  arrayFilters: [
    {
      "answer.srno": 10
    },
    {
      "userMtc.anssrno": 9
    }
  ]
})
我的Spring启动版本:2.4.0 MongoDB版本:4.2.3 在我的java代码中添加了如下建议的更改。低于误差

Caused by: com.mongodb.MongoCommandException: Command failed with error 9 (FailedToParse): 'Error parsing array filter :: caused by :: Expected a single top-level field name, found 'answer' and 'userMtc'' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "Error parsing array filter :: caused by :: Expected a single top-level field name, found 'answer' and 'userMtc'", "code": 9, "codeName": "FailedToParse"}
添加屏幕截图(调试)-如果有帮助

您用于更新嵌套数组的用法不正确。也重构了代码

Query query = new Query(Criteria.where("id").is(question.getId()).and("questionDetail.answers.srno").is(10));
Update update = new Update().inc("questionDetail.answers.$[answer].userMatchPairAnswerCounts.$[userMtc].anscnt", 1)
        .filterArray(Criteria.where("answer.srno").is(10))
        .filterArray(Criteria.where("userMtc.anssrno").is(9));
Question questionFound = mongoTemplate.findAndModify(query, update, Question.class);

添加了一个工作示例-

非常感谢,我将给它一个tryDirect mongo命令行,它工作得很好,但是java spring boot我遇到了以下错误。原因:com.mongodb.MongoCommandException:命令失败,出现错误9(FailedToParse):“分析数组筛选器时出错::原因::应为单个顶级字段名,在服务器localhost:27017上找到了“answer”和“userMtc”。完整的响应是{“ok”:0.0,“errmsg”:“Error parsing array filter::cause by::Expected一个顶级字段名,找到'answer'和'userMtc'”,“code”:9,“codeName”:“failedtopasse”}用新的更新更新更新了我的问题,还添加了一个屏幕(debug)-以帮助感谢提供所有详细信息。更新答案-现在应该可以了。非常感谢!也接受并授予了赏金
Caused by: com.mongodb.MongoCommandException: Command failed with error 9 (FailedToParse): 'Error parsing array filter :: caused by :: Expected a single top-level field name, found 'answer' and 'userMtc'' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "Error parsing array filter :: caused by :: Expected a single top-level field name, found 'answer' and 'userMtc'", "code": 9, "codeName": "FailedToParse"}
Query query = new Query(Criteria.where("id").is(question.getId()).and("questionDetail.answers.srno").is(10));
Update update = new Update().inc("questionDetail.answers.$[answer].userMatchPairAnswerCounts.$[userMtc].anscnt", 1)
        .filterArray(Criteria.where("answer.srno").is(10))
        .filterArray(Criteria.where("userMtc.anssrno").is(9));
Question questionFound = mongoTemplate.findAndModify(query, update, Question.class);