복붙노트

[MONGODB] findOneAndUpdate가 returnNewDocument과 함께 사용했을 때 true를 돌려줍니다 원본 문서 MongoDB를

MONGODB

findOneAndUpdate가 returnNewDocument과 함께 사용했을 때 true를 돌려줍니다 원본 문서 MongoDB를

내가 노드 JS와 MongoDB를 사용하고, 나는 MongoDB를 설치 NPM 사용

내가 업데이트 된 문서를 기존 문서를 업데이트하고 반환 할 문서가 제대로 업데이트됩니다. 하지만 이전 문서를 갱신하기 전에 원본 문서를 의미 반환합니다. true 매개 변수를하지만 사용 : 나는 returnNewDocument을 사용했다.

            var filter = {
                '_id': object_id
            },
            update = {
                $set: { "status" : data["status"] },
                $push: {
                    "statusHistory": {
                        $each: [{ status:data["status"],statusChangedTime:data["statusChangedTime"],comment:data["comment"]}],
                        $position:0,
                    }
                },
            }
            ,options = {
                //upsert: false,
                //multi: false,
                returnNewDocument: true
            };

            col.findOneAndUpdate(filter, update, options,function(err, res) {
                if (err) {

                    console.log(err);
                }else {

                    console.log(res);
                }
            });

응답은

{ lastErrorObject: { updatedExisting: true, n: 1 },
  value: 
   { 
//original document
   },     
  ok: 1 }

내가 직접 단자와 시도를 통해 MongoDB를 갈 때

db.MyCollection.find().pretty();

문서는 그냥 대신 업데이트 된 하나의 원본을 반환 올바르게 업데이트됩니다.

2 시간 동안 여기 붙어, 어떤 도움에 감사드립니다

package.json에서

"mongodb": "^2.1.4",

해결법

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

    1.Node.js를 드라이버 설명서는 (동일한 이름을 가진 MongoDB의 쉘 명령에 대한 옵션입니다) findOneAndUpdate ()에 대한 returnNewDocument 옵션을 언급하지 않습니다.

    Node.js를 드라이버 설명서는 (동일한 이름을 가진 MongoDB의 쉘 명령에 대한 옵션입니다) findOneAndUpdate ()에 대한 returnNewDocument 옵션을 언급하지 않습니다.

    대신, returnOriginal라는 옵션, 디폴트는 true를 언급하고있다. 대신 원래의 업데이트 된 문서를 반환 false로 설정,이 옵션을 사용해보십시오.

  2. ==============================

    2.이 최신 2018을보고하는 경우도 returnNewDocument : 사실도 returnOriginal : 거짓, 작동합니다. 대신 같이 참으로 새로운라는 속성을 설정해야합니다 {..., 새로운 : TRUE} 쿼리에 대한 옵션 개체있다.

    이 최신 2018을보고하는 경우도 returnNewDocument : 사실도 returnOriginal : 거짓, 작동합니다. 대신 같이 참으로 새로운라는 속성을 설정해야합니다 {..., 새로운 : TRUE} 쿼리에 대한 옵션 개체있다.

  3. ==============================

    3.NodeJS MongoDB의 드라이버는 명령 findOneAndUpdate ()에 대한 기본 MongoDB의 쉘에 비해 다른 인수가 있습니다. 다음 "^ 2.1.4"사용 : "MongoDB의"를 사용하는 경우

    NodeJS MongoDB의 드라이버는 명령 findOneAndUpdate ()에 대한 기본 MongoDB의 쉘에 비해 다른 인수가 있습니다. 다음 "^ 2.1.4"사용 : "MongoDB의"를 사용하는 경우

    대신

    .

    의 코드 아래 보자 :

    db.collection('user_setting').findOneAndUpdate({user_id: data.user_id}, {$set: data}, {projection: dbConfig.userSetting, returnOriginal: false}, function (err, res) {
            if (err) {
                callback({'error': 1, 'message': 'Internal server error! ' + err, 'data': null, 'status': 500});
            }   else {
                     console.log(res);
                    /* { lastErrorObject: { updatedExisting: true, n: 1 },
                          value: 
                               { user_id: 1,
                                 notification_alert: 1,
                                 notification_sound: 1,
                                 user_setting_id: 2 
                                },
                           ok: 1 
                      }      */       
            }
        });
    
  4. ==============================

    4.만약이 문제에 다른 사람으로 실행하여 {returnOriginal : 거짓}는 몽구스 설정에서 :

    만약이 문제에 다른 사람으로 실행하여 {returnOriginal : 거짓}는 몽구스 설정에서 :

    {새로운 : TRUE} 몽구스 사용하는 대신 {returnOriginal : 거짓}.

    이 기능은 여기 참조 : 몽구스 : findOneAndUpdate 업데이트 문서와 몽구스의 문서 내에 반환하지 않습니다 :

    사용하는 경우 그래서 findOneAndUpdate 새로운 추가 : 사실 메소드 옵션 :

    ...
    
    const options = {
      new: true
    };
    
    col.findOneAndUpdate(filter, update, options, function (err, res) {
      if (err) {
        console.log(err);
      } else {
        console.log(res);
      }
    });
    
  5. from https://stackoverflow.com/questions/35626040/findoneandupdate-used-with-returnnewdocumenttrue-returns-the-original-document by cc-by-sa and MIT license