更新嵌套文档参考

时间:2019-03-26 20:48:19

标签: mongodb mongoose

我有一个名为MentorProfile的文档,其中引用了文档CommentFeed。我正在执行以下操作:

MentorProfile.findOneAndUpdate(
        {
            _id: req.body.profileId,
            'lessons._id': req.body.lessonId,
            'lessons.completedList.userData': { $ne: req.user._id }
        },
        {
            $push: {
                'lessons.$.completedList': {
                    name: req.user.name,
                    avatar: req.body.userAvatar,
                    userRecord: req.user._id
                }
            },
            $push: {
                'lessons.$.commentFeed.comments': {
                    user:{name:'name', avatar:'avatar', userRecord:'8u38u3jfj'}
                    text: 'test'
                }
            }
        },
        { new: true }
    )

我想向注释数组中推送新的注释,该数组嵌套如下:

  
    

MentorProfile> CommentFeed>评论

  

我所附的代码段无效,我确定我缺少明显的内容

MentorProfile相关信息摘要

lessons: [
        {
            completedList: [
                {
                    name: { type: String, required: true },
                    avatar: { type: String, required: true },
                    userData: {
                        type: Schema.Types.ObjectId,
                        ref: 'User'
                    }
                }
            ],
            stepType: { type: String, required: true },
            commentFeed: { type: Schema.Types.ObjectId, ref: 'CommentFeed', required: true }
        }
    ]

CommentFeed代码段

comments: [
        {
            user: {
                name: { type: String, required: true },
                avatar: { type: String, required: true },
                userRecord: {
                    type: Schema.Types.ObjectId,
                    ref: 'User'
                }
            },
            text: { type: String, required: true }]}

0 个答案:

没有答案