2012-07-01 2 views
0

내 코드에 다음과 같은 추종자 추가 기능을하고 있습니다. 여기에 코드가 있습니다.Mongoose : MongoDB 추종자 및 다음 구조의 업데이트

요약하면 작업 흐름은 다음과 같습니다. 다음 사람의 ObjectId를 추가하는 추종자 배열을 업데이트합니다. 반대의 경우도 마찬가지입니다. 첫 번째 업데이트시에는 내 문서에서 0이 반환되고, 어떤 힌트가 있니? 업데이트하는 옵션에 {safe:true}을 전달하여

if (typeof(req.body.userToFollow) != 'undefined') { 
    var conditions = { _id: req.user._id } 
    , update = {"$addToSet":{following: req.body.userToFollow}} 
    , options = {}; 


    Users.update(conditions, update, options, function(err, doc){ 
     if (!err && doc) { 

      var reconditions = { _id: req.body.userToFollow } 
      , reupdate = {"$addToSet": { followers : req.user._id } } 
      , reoptions = {}; 
      Users.update(conditions, update, options, function(err, doc){ 
       if (!err && doc) { 
        var body = JSON.stringify(doc); 
        res.header('Content-Type', 'application/json'); 
        res.end(body); 
       } else { 
        console.log(err); 
       } 
      }); 
     } else { 
      console.log("\n\n\n\n\n"); 
      console.log(doc); 
     } 
    }); 

} 

많은 감사

답변

1

를 사용하여 안전 모드(). 그런 다음 업데이트 상태를 확인할 수 있습니다.