首页 > 解决方案 > MongoTemplate Criteria Criteria for Query 以更新对象数组中的元素

问题描述

我正在尝试为特定答案推送评论,我尝试了各种方法,但评论总是被添加到父问题中,而不是针对该问题中的答案。

我的数据库层次结构:

  • 问题1
    • 评论1
    • 评论2
    • 答案1
      • CommentForAnswer1
    • 答案2

| 在此处输入图像描述 |

我的模型类

问题.java

@Document(collection = "QUESTION")
public class QUESTION {
    @Id
    private String questionId;
    private String questionHeader;
    private String questionBody;
    private String byUser; //username
    private Instant postedAt;
    private int upVotes;
    private int downVotes;
    private List<ANSWER> answerObj;
    private List<COMMENT> commentObj;

答案.java

public class ANSWER {
    private String answerId;
    private String answerBody;
    private String byUser; //username
    private Instant postedAt;
    private int upVotes;
    private int downVotes;
    private List<COMMENT> commentObj;

COMMENT.java

public class COMMENT {
    private String commentId;
    private String commentBody;
    private String byUser; //username
    private Instant postedAt;
    private int upVotes;
    private int downVotes;

控制器

@PostMapping(value = "posts/{questionId}/add")
    public ResponseEntity<?> postComment(@PathVariable String questionId,
                                         @RequestParam(value = "answerId", defaultValue = "") String answerId,
                                         @RequestBody PostCommentRequest postCommentRequest,
                                         Authentication authentication) {
        String username = authentication.getName();
        PostCommentResponse postCommentResponse = postService.postAComment(postCommentRequest, username, questionId, answerId);
        return ResponseEntity.ok().body(postCommentResponse.getMessage());
    }

存储库

update.push("commentObj", comment);
try {
    query.addCriteria(answerId.isEmpty() ?
            (new Criteria("questionId").is(questionId)) :
            (new Criteria("answerObj.answerId").is(answerId).andOperator(Criteria.where("questionId").is(questionId))));
    mongoTemplate.updateFirst(query, update, QUESTION.class, QUESTION_COLLECTION);
    return new PostCommentResponse("Successfully updated!", comment);
} catch (Exception e) {
    return new PostCommentResponse("Failed to update, Reason: " + e.toString(), null);
}

有人可以帮我找出可以将对象推送到另一个对象数组中的查询条件。

编辑:MongoDB 4.0 版(最新)

标签: databasemongodbspring-bootnosqlmongotemplate

解决方案


这是我对这种情况的建议:

public Mono<ProjectChild> UpdateCritTemplChild(
       String id, String idch, String ownername) {

    Query query = new Query();
    query.addCriteria(Criteria.where("_id")
                              .is(id));
    query.addCriteria(Criteria.where("tasks._id")
                              .is(idch));

    Update update = new Update();
    update.set("tasks.$.ownername", ownername);

    return template
         // findAndModify:
         // Find/modify/get the "new object" from a single operation.
         .findAndModify(
              query, update,
              new FindAndModifyOptions().returnNew(true), ProjectChild.class
                       )
         ;

  }


推荐阅读