微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

猫鼬嵌套数组更新

如何解决猫鼬嵌套数组更新

嘿,这就是我的架构。

const socialSchema = new mongoose.Schema({
    Entry_Number: Number,Player_Name: String,Player_Message : String,Player_Fullmessage: String,Player_ProfilePic : String,Player_UploadedPic: String,Player_UploadedVideos: String,Status_Date: String,Status_Feeling: String,Google_Profile: String,Status_Likes: [{Like_Number: Number,Liked_By: String}],Status_Comments: [{ 
                        Comment_Number: Number,Comment_By: String,Comment_Message: String,replies: [
                          {Comment_By: String,Comment_Message: String}
                        ]
                     }],Friends: { type: Array,default: []},Shares: { type: Array,Shared_Post: String,Shared_Newmessage: String,Shared_PostDetails: [{type: Schema.Types.ObjectId,ref: "socialdatas"}]
});

我正在尝试从前端的值更新Status_Comments数组中的嵌套嵌套数组。这是我经过多次尝试后的位置。

socialRegister.findOneAndUpdate({_id: req.body.hiddentextcommentid3,"Status_Comments._id": req.body.hiddentextcommentid3a},{$push: {"Status_Comments.$.replies":
                                                                                              {
                                                                                                  "Comment_By": req.user.Player_Name,"Comment_Message" : req.body.replymessage
                                                                                              }}},function(err,data1)

我在终端上没有任何错误,但是也没有更新发生。有人可以帮忙吗?

解决方法

尝试这个:

let reply = {
              "Comment_By": req.user.Player_Name,"Comment_Message" : req.body.replymessage
            }
socialRegister.findOneAndUpdate({"Status_Comments._id":req.body.hiddentextcommentid3a},{$push: {"Status_Comments.$.replies": reply }},{new: true},(err,data) =>{
                                          console.log(data)
                                 })

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。