CNode

mongoose如何给数组子集的子集插入数据

问答
GGil2015发布于8 年前最后回复8 年前1 回复2914 浏览0 收藏

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)

Z
zswnew#1·8 年前

用Scheme.Types.Mixed

来自酷炫的 CNodeMD

参与回复
登录后即可参与回复。登录