如何从post()更新mongoDB

时间:2015-05-24 05:10:51

标签: angularjs node.js mongodb express

我正在创建一个MEAN Stack应用程序。我的帖子功能如下:

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);
        }
    }
 );
});

当我在我的前端Angular代码中访问此函数时我的

console.log(result);

出来:

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

但我应该看到n:1表示有更新?为什么我的Node应用程序没有更新我的mongoDB密钥:值对?

我有什么关于db.collection.update()的东西吗?

1 个答案:

答案 0 :(得分:0)

我能够弄清楚:

分配_id查询时,我需要这样做:

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