猫鼬嵌套数组更新

时间:2020-08-13 13:08:15

标签: mongodb mongoose

嘿,这就是我的架构。

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, default: []},
    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)

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

1 个答案:

答案 0 :(得分:0)

尝试这个:

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)
                                 })