복붙노트

[MONGODB] NodeJS와 MongoDB를 사용하여 FindAndModify () 필요 삭제 또는 업데이트

MONGODB

NodeJS와 MongoDB를 사용하여 FindAndModify () 필요 삭제 또는 업데이트

노드 JS와 MongoDB를에 findAndModify을하려고 메신저, 이건 내 코드입니다 :

var nextBill = function (db, success, log) {
    var collection = db.collection('autoincrements');
    log.debug('autoIncrementRepository', 'nextBill');
    var result = collection.findAndModify({
        query: { _id: 'auto' },
        update: { $inc: { bill: 1 } },
        new: true
    });

    success(result.bill);
};

편집하다:

콜백 시도

collection.findAndModify({
        query: { _id: 'auto' },
        update: { $inc: { bill: 1 } },
        new: true
    }, function (e, result) {
        success(result.budget);
    });

그러나 그 일을 나에게 오류 필요성 삭제 또는 update..But 메신저를주고 ..

해결법

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

    1.노드 고유 드라이버 구현에서 .findAndModify () 메소드는 몽고 쉘 구현 다르다. 당신이 위와 같이 업데이 트를 수행합니다 :

    노드 고유 드라이버 구현에서 .findAndModify () 메소드는 몽고 쉘 구현 다르다. 당신이 위와 같이 업데이 트를 수행합니다 :

    collection.findAndModify(
       { "_id": "auto" },
       { "$inc": { "bill": 1 } },
       function(err,doc) {
         // work here
    
       }
    );
    

    이상하게도 약간은 옵션에서 지정한 제거하기 위해 같은 것 "제거"일치하는 문서 그래서 :

    collection.findAndModify(
       { "_id": "auto" },
       { "$inc": { "bill": 1 } },
       { "remove": true },
       function(err,doc) {
         // work here
    
       }
    );
    

    가장 큰 차이는 작업의 "키"섹션의 이름을하지 않는 것.

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

    2.http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify

    http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify

    두 번째 매개 변수는 여러 사람이 쿼리와 일치하는 경우에 사용할 문서 선택의 정렬 순서입니다이 위의 문서를 지정합니다. 만 "제거 또는 업데이트 필요"오류 메시지가 표시됩니다 두 개의 매개 변수를 제공합니다.

    collection('MyCollection').findAndModify(
        { _id: "auto" },
        [],
        { $inc: { "bill": 1 } },
        { upsert: true, new: true },
        function(err,doc) {
           // work here
        }
    );
    
  3. ==============================

    3.

    Hi I have followed this and it worked perfectly.
    
    db.collection('test').findAndModify(
      {hello: 'world'}, // query
      [['_id','asc']],  // sort order
      {$set: {hi: 'there'}}, // replacement, replaces only the field "hi"
      {}, // options
      function(err, object) {
          if (err){
              console.warn(err.message);  // returns error if no matching object found
          }else{
              console.dir(object);
          }
      });
    });
    
  4. ==============================

    4.이 그것은 nodejs에서 나를 위해 일한 시도

    이 그것은 nodejs에서 나를 위해 일한 시도

    users.findAndModify(
               { "_id": userid,"password":pwd},
               [['_id', 'asc']],
               { "$set":{"password":npwd}},
               {"upsert":false}
            ,function(err,result){
            //enter code here
    
        })
    
  5. from https://stackoverflow.com/questions/24325687/nodejs-and-mongodb-findandmodify-need-remove-or-update by cc-by-sa and MIT license