I encountered the same issue TODAY.
My element class is :
@Data
@Document(collection = "comment")
public class Comment {
@Id
private String id;
private Integer bookId;
private String username;
private String content;
private Date time;
private List<Comment> replies;
private String replyTo;
public Comment() {
// not used
}
public Comment(Integer bookId, String username, String content, String reply_to) {
this.bookId = bookId;
this.username = username;
this.content = content;
this.time = new Date();
this.replies = new ArrayList<>();
this.replyTo = reply_to;
}
}
Initially, I tried to updating a comment’s reply list using MongoRepository.save method. It, however , doesn’t update anything.
Then, I referenced this post and manually write a updateComment method to update:
@Repository
public interface BookCommentRepository extends MongoRepository<Comment, String> {
List<Comment> findByBookId(Integer bookId);
@Query("{ 'id': ?0 }")
@Update("{ '$set': {'replies': ?1 }}")
void updateComments(String id, List<Comment> comments);
}
It does works. ![]()