2017-12-24 11 views
0

가 좀 문제가 작동하지 삭제할 경우 은 "iduser을"유효하지 않거나 유효하고 객체 수정 문서에서 삭제되지 "수정 삭제"하고 계정 문서MongoDB를 풀/배열 에 항상 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}); 
삭제 0

및 :)

+0

가능한 복제를 사용할 수 있습니다 - 많은 참조 - 몽구스] (https://stackoverflow.com/questions/32674280/removing-one-one-and-one-many-references-mongoose) – Veeram

답변

0

을 도와 계정 개체에서

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

도움 주셔서 감사하지만 그 기술은 " Account.modification "전용 (_id). "Modification doc"에있는 실제 객체는 여전히 존재합니다. 내 스키마 좀 봐. –