2016-02-26 56 views
0

我有以下組合,NodeJS,Express,MongoDB和Mongoose。我已經實施了與貓鼬承諾允許同時編輯的隨機數。請參閱以下:NodeJS,ExpressJS,Mongoose 4.4,Nonce用於併發和Document.update

//schema 
var item_schema = { 
    //_id: {type: Schema.ObjectId, required: true}, 
    name: {type: String, required: true, index: { unique: true }}, 
    code: {type: String, required: true, index: { unique: true }}, 
    date_time_created: {type: Date, required: true}, 
    date_time_updated: {type: Date, required: true}, 
    nonce: {type: Schema.ObjectId} 
}; 

//model 
var item_model = mongoose.model('item', schema); 

//update 
var concurrency_update = function(req, res, retries) { 

    var promise = model.findById(req.params.id).exec(); 

    var updated_nonce = mongoose.Types.ObjectId(); 

    promise.then(function(document){ //find document response 

     if(!document) { 
      res.status = 404; 
      return Promise.reject({ "message" : req.params.id + ' document does not exist' }); 
     } 
     var now = new Date(); 
     if(req.body.code) { 
      document.code = req.body.code; 
      document.date_time_updated = now; 
     } 
     if(req.body.name) { 
      document.name = req.body.name; 
      document.date_time_updated = now; 
     } 
     if(!document.nonce) { 
      document.nonce = updated_nonce; 
      var old_nonce = document.nonce; 
     } 
     else { 
      var old_nonce = document.nonce; 
      document.nonce = updated_nonce; 
     } 
     return document.update({ "_id" : req.params.id, "nonce" : old_nonce }).exec(); 

    }).then(function(raw){ //update response 

     var number_affected = raw.n; 
     console.log(raw); 
     if(!number_affected && retries < 10){ 
      //we weren't able to update the doc because someone else modified it first, retry 
      console.log("Unable to update, retrying ", retries); 
      //retry with a little delay 
      setTimeout(function(){ 
       concurrency_update(req, res, (retries + 1)); 
      }, 20); 
     } else if (retries >= 10){ 
      //there is probably something wrong, just return an error 
      return Promise.reject({ "message" : "Couldn't update document after 10 retries in update"}); 
     } else { 
      res.json({"message": req.params.id + ' document was update'}); 
     } 

    }).catch(function(err){ 
     res.send(err.message); 
    }); 

併發更新是基於的是: http://www.mattpalmerlee.com/2014/03/22/a-pattern-for-handling-concurrent/

和閱讀貓鼬文檔更新是基於關閉了這一點。但是,當代碼進入最終的.then(/ /更新響應)時,我看到raw.n(numberAffected)= 1但數據庫永遠不會更新?

答案可能很接近但我很想念它。 我對此有什麼想法?

+1

無論你做什麼,如果你首先使用任何類型的'.findOne()'變體從數據庫中檢索,你將會遇到問題。請使用['.update()'](http://mongoosejs.com/docs/api.html#model_Model.update)或['.findOneAndUpdate()'](http://mongoosejs.com/docs/ api.html#model_Model.findOneAndUpdate)變體以及[原子更新修飾符](https://docs.mongodb.org/manual/reference/operator/update-field/)。這些都是爲了在當前狀態下「就地」修改,而不用擔心在獲取和重新保存數據之間可能發生的情況。改變你的模式。 –

+0

@blake_seven - 所以如果我理解你的評論,如果你使用原子更新修飾符(例如$ set和$ remove),那麼nonce是不需要的。我會嘗試併發布解決方案。 – thxmike

回答

0

在@blakes_seven發表評論後,我能夠刪除nonce的使用並使用原子修飾符應用更新。這是更新後的測試代碼。

//schema 
var item_schema = { 
    //_id: {type: Schema.ObjectId, required: true}, 
    name: {type: String, required: true, index: { unique: true }}, 
    code: {type: String, required: true, index: { unique: true }}, 
    date_time_created: {type: Date, required: true}, 
    date_time_updated: {type: Date, required: true}, 
    nonce: {type: Schema.ObjectId} 
}; 

//model 
var item_model = mongoose.model('item', schema); 

//update 
var concurrency_update = function(req, res, retries) { 

    var updated_data = {}; 
    if(req.body.code) { 
     updated_data.code = req.body.code; 
    } 
    if(req.body.name) { 
     updated_data.name = req.body.name; 
    } 
    if(!req.body.name.nonce) { 
     updated_data.nonce = mongoose.Types.ObjectId(); 
    } 

    if(updated_data !== {}) { 
     var update = { 
      $currentDate: { 
       date_time_updated: true 
      }, 
      $set: updated_data 
     }; 
     var promise = model.update({"_id": req.params.id}, update).exec(); 

     promise.then(function (raw) { 

      var number_affected = raw.nModified; 
      console.log(raw); 
      if (!number_affected && retries < 10) { 
       //we weren't able to update the doc because someone else modified it first, retry 
       console.log("Unable to update, retrying ", retries); 
       //retry with a little delay 
       setTimeout(function() { 
        concurrency_update(req, res, (retries + 1)); 
       }, 20); 
      } else if (retries >= 10) { 
       //there is probably something wrong, just return an error 
       return Promise.reject({"message": "Couldn't update document after 10 retries in update"}); 
      } else { 
       res.json({"message": req.params.id + ' document was update'}); 
      } 

     }).catch(function (err) { 
      res.send(err.message); 
     }); 
    } 
    else { 
     res.status = 400; 
     res.send({"message": 'There is nothing specified in the payload to update!'}) 
    } 
};