MongoDB嵌套文档的一些操作

想要一个这样的数据结构:

{
    "_id" : "52352525-b904-4b86-968a-8e80071da6b9",
    "data" : [ 
        {
            "commentfrom_userid" : "aaa",
            "commentto_username" : "评论谁的1241234",
            "comment" : "评论内容5435",
            "_class" : "cn.asxuexi.entity.Article_Id",
            "commentList" : [ 
                {
                    "commentfrom_userid" : "aaa",
                    "commentto_username" : "评论谁的1241234",
                    "comment" : "评论内容5435",
                    "_class" : "cn.asxuexi.entity.Article_Id"
                }, 
                {
                    "commentfrom_userid" : "bbb",
                    "commentto_username" : "评论谁的1241234",
                    "comment" : "评论内容5435",
                    "_class" : "cn.asxuexi.entity.Article_Id"
                }, 
                {
                    "commentfrom_userid" : "bbb",
                    "commentto_username" : "评论谁的",
                    "comment" : "评论内容",
                    "_class" : "cn.asxuexi.entity.Article_Id"
                }
            ]
        }
    ]
}

实体类Article_Id :

public class Article_Id {
    private String commentfrom_userid;//评论用户id
    private String commentto_username;//被评论人名字
    private String comment;             //评论内容
    private String commentfrom_orgname;//机构名字
    private String commentfrom_logo;//机构头像
    private String commentfrom_username;//用户名字
    private String commentfrom_userphoto;//用户头像
    private List<CommentList>  commentList;//二级评论
  // get set····省略
}

实体类:CommentList

public class CommentList {
    private String commentfrom_userid;//评论用户id
    private String commentto_username;//被评论人名字
    private String comment;             //评论内容
    private String commentfrom_orgname;//机构名字
    private String commentfrom_logo;//机构头像
    private String commentfrom_username;//用户名字
    private String commentfrom_userphoto;//用户头像
  // get set····省略
}

实体类:CommentData

public class CommentData {
    @Id
    private String article_id;  //文章id
    private List<Article_Id> data;
 // get set····省略
}

一级评论:

   public int addComment(Article_Id article_id, String articleid) {
        int commentfhz;
        int identify = findDao.getIdentify(articleid);//查询是否存在此篇文章
        if (identify == 1) {
            try {
                Query query = Query.query(Criteria.where("_id").is(articleid));//根据传来的id查看是否存在
                Update update = new Update();
                //update.push("Students", student);//addToSet如果数据已经存在,则不做任何操作,而push会插入一条一样的数据。
                update.addToSet("data", article_id);//存在则向content字段添加新的集合
                mongoTemplate.upsert(query, update, "commentData");
                commentfhz = 600;
            } catch (Exception e) {
                commentfhz = 400;
                e.printStackTrace();
            }
        } else {
            commentfhz = 400;
        }
        return commentfhz;
    }

这样MongoDB数据库中就存入了这样的结构

{
 "_id" : "52352525-b904-4b86-968a-8e80071da6b9",
 "data" : [ 
        {
            "commentfrom_userid" : "aaa",
            "commentto_username" : "评论谁的1241234",
            "comment" : "评论内容5435",
            "_class" : "cn.asxuexi.entity.Article_Id",}
]
}

二级评论:

  public int twoComment(CommentList commentList, String articleid) {
        String userId = (String) Token_JWT.verifyToken().get("userId");//获取当前登录用户
        int commentfhz;
        int identify = findDao.getIdentify(articleid);//查询是否存在此篇文章
        if (identify == 1) {
            try {
                Query query = Query.query(Criteria.where("_id").is(articleid).and("data.commentfrom_userid").is("dd"));//根据传来的id查看是否存在
                Update update = new Update();
                //update.push("Students", student);//addToSet如果数据已经存在,则不做任何操作,而push会插入一条一样的数据。
                update.addToSet("data.$.commentList", commentList);//存在则向content字段添加新的集合
                mongoTemplate.upsert(query, update, "commentData");
                commentfhz = 600;
            } catch (Exception e) {
                commentfhz = 400;
                e.printStackTrace();
            }
        } else {
            commentfhz = 400;
        }
        return commentfhz;
    }

Criteria.where("_id").is(articleid).and("data.commentfrom_userid").is("dd"),这就是两个判断条件,锁定到某一条,
update.addToSet("data..commentList", commentList) data..commentList,就是向 data数组中在添加一个数组,
得到了这样的结构

{
    "_id" : "52352525-b904-4b86-968a-8e80071da6b9",
    "data" : [ 
        {
            "commentfrom_userid" : "dd",
            "commentto_username" : "评论谁的1241234",
            "comment" : "评论内容5435",
            "_class" : "cn.asxuexi.entity.Article_Id",
            "commentList" : [ 
                {
                    "commentfrom_userid" : "bbb",
                    "commentto_username" : "评论谁的1241234",
                    "comment" : "评论内容5435",
                    "_class" : "cn.asxuexi.entity.CommentList"
                }
            ]
        }
    ]
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容