最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mongodb拉删除不起作用

运维笔记admin8浏览0评论

mongodb拉/删除不起作用

mongodb拉/删除不起作用

我有一些问题,删除数组中的对象,如果“iduser”无效或有效且该对象未在Modification doc和Account doc中删除,则该API的结果始终为“修改已删除”

这是我的主要文件

let Account = new Schema({
  firstName       : {type : String, required : true},
  lastName        : String,
  email           : {type : String, required : true},
  profilePicture  : {type : String, required : true},
  gender          : {type : String, required : true},
  id              : {type : String, required : true},
  location        : String,
  birthday        : Date,
  experiences     : [Number],
  achievements    : [Number],
  modifications   : {type : [Schema.Types.ObjectId], ref : 'Modification'}
},{usePushEach: true});

这个我要删除的数组对象

let modificationSchema = new Schema({
  desc      : {type : String, required : true},
  make      : {type : String, required : true},
  type      : {type : String, required : true},
  photo     : {type : String, required : true},
  postDate  : {type : Date,   required : true},
  likes     : {type : [Schema.Types.ObjectId], ref : 'Like'},
  comments  : {type : [Schema.Types.ObjectId], ref : 'Comment'}
},{usePushEach: true});

这是我从Account对象中删除修改对象的代码

api.put('/modification/:iduser/:idmodif' , (req,res) =>{
    Account.findOneAndUpdate({"email":req.params.iduser},
      { $pull : { modifications : { _id : req.params.idmodif}}}, 
function(err,model){
        if(err){
          return res.send(err);
        }else{
          res.json({message : "modification deleted"});
        }
      }
    );
  });

谢谢你的帮助:)

回答如下:

你可以使用标准技术

api.put('/modification/:iduser/:idmodif' , (req,res) =>{
    Account.findById({_id:req.params.iduser}, 
function(err,model){
        model.modifications = null;
        model.save(function(err,result){
        if(err){
          return res.send(err);
        }else{
          res.json({message : "modification deleted"});
        }
        });
      }
    );
  });

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论