【问题标题】:how to find by subdocument and document and the update is pushing a subdocument如何通过子文档和文档查找并且更新正在推送子文档
【发布时间】:2019-09-03 03:00:50
【问题描述】:

所以我有这个模型

const postSchema = new mongoose.Schema({
    image: [{type: String, required: true, unique: true}],
    comments: [{
        id: {type: mongoose.Schema.Types.ObjectId},
        user: {type: mongoose.Schema.Types.ObjectId, ref: 'user'},
        comments: {type: String},
        like: [{type: mongoose.Schema.Types.ObjectId, ref: 'user'}],
        time: {type: Date, default: Date.now}
    }],
    caption: {type: String},
    user: {type: mongoose.Schema.Types.ObjectId, ref: 'user', required: true},
    like: [{type: mongoose.Schema.Types.ObjectId, ref: 'user'}],
    status: {type: Number,default: 0}
}, {timestamps: true});

我要做的是根据 cmets 的 id 推送 cmets' like 我试过了

Post.findOneAndUpdate({
            _id:req.body.postid,
            'comments.id':req.body.id
        }, {
            $push :{
                "comments.$.like" : res.userdata.id
            }
        }, {}, err => {
            if(err){
                res.status(500).json({
                    err: err
                })
            }else {
                res.status(200).json({
                    message: "success",
                });
            }
        });

但它不起作用..更糟糕的是这是我找到帖子时的文档

"_id" : ObjectId("5d6d4754599d8b06c01b1894"),
    "image" : [
            "2019-09-02T16-46-11.835Z123 - Copy.jpg",
            "2019-09-02T16-46-11.892Z123.jpg",
            "2019-09-02T16-46-11.938Z8358-dragon-1680x1050-digital-art-wallpaper - Copy.jpg",
            "2019-09-02T16-46-11.943Z8358-dragon-1680x1050-digital-art-wallpaper.jpg",
            "2019-09-02T16-46-11.949Zasd.jpg",
            "2019-09-02T16-46-11.979Zasd1.jpg",
            "2019-09-02T16-46-11.991Zwallpaper2you_567134.jpg"
    ],
    "like" : [ ],
    "status" : 0,
    "caption" : "asd",
    "user" : ObjectId("5d6c8d934a401d3748ef0c3e"),
    "comments" : [
            {
                    "like" : [ ],
                    "_id" : ObjectId("5d6d476f599d8b06c01b189a"),
                    "time" : ISODate("2019-09-02T16:46:39.499Z")
            }
    ],
    "createdAt" : ISODate("2019-09-02T16:46:12.034Z"),
    "updatedAt" : ISODate("2019-09-02T16:46:39.498Z"),
    "__v" : 0

idk 为什么 user 和 cmets 子文档被删除,甚至 like 字段不推送

注意:我的猫鼬依赖"mongoose": "^5.6.4",

有什么建议吗? 对不起我的英语不好

【问题讨论】:

    标签: arrays mongodb express mongoose mongodb-query


    【解决方案1】:

    你可以通过以下方法使用,

    Post.findOne({_id:req.body.postid,
                'comments.id':req.body.id })
                .exec((err , foundObject)=>{
                   if(err){
                       console.log(err);
                   }else{
                     console.log("Object ===>" . object);
                     object.comments.push( res.userdata.id);
                     object.save(function (err) {
                       if(err) {
                        console.error('ERROR!');
                       }
                     }); 
                   }
                });
    

    【讨论】:

      【解决方案2】:

      你确定req.body.postidres.userdata.idreq.body.id的类型是ObjectId吗?试试这个方法

      Post.findOneAndUpdate(
          {
              _id: ObjectId(req.body.postid),
              comments.id: ObjectId(req.body.id)
          }, 
          {
              $push: {
                  comments.like: ObjectId(res.userdata.id)
              }
          }, {}, err => {
              if (err) {
                  res.status(500).json({
                      err: err
                  })
              } else {
                  res.status(200).json({
                      message: "success",
                  });
              }
          }
      );
      

      【讨论】:

        猜你喜欢
        • 2018-10-26
        • 2021-02-25
        • 2020-09-08
        • 2014-11-27
        • 2016-03-24
        • 2020-11-29
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多