2014-05-10 5 views
1

문서를 복사하려고합니다. 처음에 찾았습니다. 그런 다음 _id를 제거하십시오. 그런 다음 삽입하십시오. 하지만 계산은 여전히 ​​진행 중입니다. 중복 오류가 발생합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?몽구스 (mongo), 문서 복사

mongoose.model('calculations').findOne({calcId:req.params.calcId}, function(){ 
     if(err) handleErr(err, res, 'Something went wrong when trying to find calculation by id'); 

     delete calculation._id; 
     console.log(calculation); //The _.id is still there 
     mongoose.model('calculations').create(calculation, function(err, stat){ 
     if(err) handleErr(err, res, 'Something went wrong when trying to copy a calculation'); 
     res.send(200); 
     }) 
    }); 

답변

6

findOne에서 반환 된 객체는 일반 객체가 아니라 몽구스 문서입니다. 이것을 {lean:true} 옵션 또는 .toObject() 메소드를 사용하여 일반 JavaScript 객체로 변환해야합니다.

mongoose.model('calculations').findOne({calcId:req.params.calcId}, function(err,calculation){ 
    if(err) handleErr(err, res, 'Something went wrong when trying to find calculation by id'); 

    var plainCalculation = calculation.toObject(); 


    delete plainCalculation._id; 
    console.log(plainCalculation); //no _id here 
});