【问题标题】:MongoTemplate Criteria for Query to update element inside array of objectsMongoTemplate Criteria Criteria for Query 以更新对象数组中的元素
【发布时间】:2020-02-09 18:46:10
【问题描述】:

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

我的数据库层次结构:

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

| |

我的模型类

QUESTION.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;

ANSWER.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 版(最新)

【问题讨论】:

标签: database mongodb spring-boot nosql mongotemplate


【解决方案1】:

这是我对这个案例的建议:

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

  }

【讨论】:

    猜你喜欢
    • 2019-07-27
    • 2012-09-07
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    相关资源
    最近更新 更多