2016-07-22 35 views
0
  • 我已經寫在使用MongoDB的 作爲後端數據庫node.js的多個更新的REST API。
  • 這些更新的API是原子的性質和MongoDB的文檔的 時間更新各個部分用戶配置文件
    例如:位置API,個人信息等API

我要找能夠知道哪些單個字段在文檔中被修改。例如,如果位置API包含國家,州,城市以及更新是否確實是針對國家/地區的。我想跟蹤國家作爲唯一的領域變化。正好與貓鼬更新結果這領域得到了修改

Mongoose不報告真正在文檔中修改的字段。

下面是示例:

Model.users.update({_id: "12345"}, {$set: update}, {upsert:true}, function (err, resp) { 
     if (err) { return console.log(err); 
---- 
}); 

響應:

{ ok: 1, 
    nModified: 1, 
    n: 1, 
    lastOp: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1469188533 }, 
    electionId: 5765d40f7dd9376cd6345122 

}

+0

使用[**'findOneAndUpdate()'**](http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate)API – chridam

+0

不起作用。你有什麼例子嗎? – user2596892

回答

0

使用貓鼬中間件用於此目的

http://mongoosejs.com/docs/middleware.html

schema.post('update', function(doc, next) { 

     // this._update.$set contains all the fields which are updated 
     if (this._update.$set.country) { 
      concole.log('country is modified'); 
     } 




    }); 
+0

我如何在這裏通過我的條件? – user2596892

+0

好吧,現在我明白,這就像對象上的觸發器一樣。這段代碼將會聽取更改。但我想知道值是否真的更新。 – user2596892

+0

if(this._update. $ set.country)檢查值是否被修改 –