복붙노트

[MONGODB] 몽구스 findByIdAndUpdate 올바른 모델을 반환하지

MONGODB

몽구스 findByIdAndUpdate 올바른 모델을 반환하지

나는 몽구스 findByIdAndUpdate 콜백 올바른 모델을 반환하지와 한 번도 본 적이 없다 문제가 있습니다.

여기 코드는 :

    var id = args._id;
    var updateObj = {updatedDate: Date.now()};
    _.extend(updateObj, args);

    Model.findByIdAndUpdate(id, updateObj, function(err, model) {
        if (err) {
            logger.error(modelString +':edit' + modelString +' - ' + err.message);
            self.emit('item:failure', 'Failed to edit ' + modelString);
            return;
        }
        self.emit('item:success', model);
    });

DB를의 원본 문서는 다음과 같습니다 :

{
    _id: 1234
    descriptors: Array[2],
    name: 'Test Name 1'
}

updateObj 이런 모습으로 가고 :

{
    _id: 1234
    descriptors: Array[2],
    name: 'Test Name 2'
}  

콜백에서 반환 된이 모델은 원래 모델이 아닌 updatedObj 동일합니다. 제가 DB를 조회 할 경우 제대로 업데이트되었습니다. 그것은 바로 데이터베이스에서 반환되는 아니에요.

는 '바보 - 사용자 오류 등이 Feel로는,하지만 난 그것을 볼 수 없습니다. 어떤 아이디어이 크게 감사합니다.

해결법

  1. ==============================

    1.몽구스 4.0 findByIdAndUpdate (및 findOneAndUpdate)의 새로운 옵션의 기본값은 (릴리스 노트 # 2262 참조) false로 변경되었습니다. 업데이트가 적용된 후, 명시 적으로 문서의 새 버전을 true로 옵션을 설정해야한다는이 수단 :

    몽구스 4.0 findByIdAndUpdate (및 findOneAndUpdate)의 새로운 옵션의 기본값은 (릴리스 노트 # 2262 참조) false로 변경되었습니다. 업데이트가 적용된 후, 명시 적으로 문서의 새 버전을 true로 옵션을 설정해야한다는이 수단 :

    Model.findByIdAndUpdate(id, updateObj, {new: true}, function(err, model) {...
    
  2. from https://stackoverflow.com/questions/30419575/mongoose-findbyidandupdate-not-returning-correct-model by cc-by-sa and MIT license