mongoose如何给数组子集的子集插入数据
 发布于 7 年前  作者 Gil2015  2772 次预览  最后一次回复是 7 年前  来自 问答 

node后台做个博客评论功能,需要给子集push数据,百度了一下知道如何更新添加第一层数据,但是子集的子集不知如何添加更改。 望求教~

// 文章数据结构
const article = {
  content: '文章内容',
  author: '作者',
  comments: [
    {
	  commentsId: 123,
      replyContent: '评论内容',
      replyUser: '评论用户',
      itemComments: [
        {
		  itemCommentId: 456
          itemReplyContent: '子评论',
          itemreplyUser: '子评论人'
        }
      ]
    }
  ]
};

// 添加数组第一层数据
articleSchema.update({
  _id: articleId
}, {
  '$push': {
    'comment': {
      replyContent: '评论内容',
      replyUser: '评论用户',
      itemComments: []
    }
  }
}, err => {
  console.log(err);
});
1 回复
zswnew

用Scheme.Types.Mixed

来自酷炫的 CNodeMD