Mongodb 嵌套文档Mongoose中仅更新第一个数组元素

Mongodb 嵌套文档Mongoose中仅更新第一个数组元素,mongodb,mongoose,mongoose-schema,nested-documents,Mongodb,Mongoose,Mongoose Schema,Nested Documents,这是我的模式: let userSchema = new mongoose.Schema({ id: String, displayName: String, displayImage: String, posts: [ { url: String, description: String, likes: [String], comments:

这是我的模式:

    let userSchema = new mongoose.Schema({
    id: String,
    displayName: String,
    displayImage: String,
    posts: [
        {
            url: String,
            description: String,
            likes: [String],
            comments: [
                { content: String, date: String, author: { id: String, displayName: String, displayImage: String } }
            ]
        }
    ]
});
我正在尝试编辑注释数组中的元素,并且已经意识到,由于MongoDB在处理双嵌套文档时功能有限,我应该创建两个单独的模式

但无论如何,这似乎对我有效,请记住,我正在尝试编辑注释数组中特定注释的内容

controller.editComment = (req, res, next) => {
User.findOne(
    { id: req.query.userid, 'posts._id': req.params.postid },
    { 'posts.$.comments._id': req.body.commentID }
)
    .exec()
    .then((doc) => {
        let thisComment = doc.posts[0].comments.filter((comment) => { return comment._id == req.body.commentID; });
        thisComment[0].content = req.body.edited;
        doc.save((err) => { if (err) throw err; });
        res.send('edited');
    })
    .catch(next);
};
这是可行的,但是它总是只更新第一篇文章的评论,不管我编辑哪个评论。但是请记住,
此注释[0]。内容
,如果console.logged,将始终在正确的帖子下显示正确注释的正确内容。但是,在
doc.save(err)
时,我假设问题发生了


非常感谢您的指导,我真的看不出有什么问题。

请参考mongoose中创建的此问题。看见