Node.js 获取Mongoose中项目推送到数组的id

Node.js 获取Mongoose中项目推送到数组的id,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,后模式,注释模式 我使用 const newPost = await Post.findByIdAndUpdate(postId, { $push: { comments: { content, }, }, }); 我想获取新评论的id,但当我console.log时,它返回未更新的帖子。我使用postman,看到新的评论被推到

后模式,注释模式

我使用

const newPost = await Post.findByIdAndUpdate(postId, {
            $push: {
                comments: {
                    content,
                },
            },
        });

我想获取新评论的id,但当我
console.log
时,它返回未更新的帖子。我使用postman,看到新的评论被推到了我的帖子上。如何获取新评论的id?

turivishal的想法:创建id评论并推送它

const commentId = new mongoose.Types.ObjectId()

await Post.findByIdAndUpdate(postId, {
            $push: {
                comments: {
                    _id: commentId
                    content,
                },
            },
        });

它解决了我的问题。但是欢迎任何其他解决方案

我的建议,最好单独生成注释id并使用新的mongoose.Types.ObjectId()推送注释
const commentId = new mongoose.Types.ObjectId()

await Post.findByIdAndUpdate(postId, {
            $push: {
                comments: {
                    _id: commentId
                    content,
                },
            },
        });