2015-05-24 43 views
0

我正在創建一個MEAN Stack應用程序。我的職位功能如下:如何從post()更新mongoDB()

app.post('/updateGroup/:id', function(req, res) { 
var id = req.params.id; // = mongoDB ObjectID ie: "55616e2a37e8728266ceac6" 
var vals = {}; 

vals['hostName'] = req.body.hostName // = a String ie, "Steve" 
             // this is a different name value than the 
             // current hostName key that is in 
             // the groupList db 

db.groupList.update(
    {"_id": id}, 
    {$set : vals}, 
    function(err, result) { 
     if (err) { 
      console.log(err); 
     } 
     else { 
      console.log(result); 
     } 
    } 
); 
}); 

當我在我的前端角碼訪問此功能我

console.log(result); 

出來爲:

{ ok: true, n: 0, updatedExisting: true } 

但我應該參見編號:1表示有更新?爲什麼我的Node應用程序不更新我的mongoDB鍵值對?

有沒有關於db.collection.update(),我錯過了什麼?

+0

蒙戈哪個版本您使用的? – ABrowne

+0

我正在使用3.0.2 – arahansen

+0

在更新時,返回writeresult,我將首先調查:http://docs.mongodb.org/manual/reference/method/WriteResult/ – ABrowne

回答

0

我能弄明白:

當分配_id查詢我需要做的是這樣的:

id['_id'] = mongojs.ObjectId(req.params.id);