使用nodejs更新mongodb集合中的文档

时间:2016-04-23 03:33:39

标签: node.js mongodb

我尝试使用nodejs更新mongo集合文档。 mongo收集文件如下:

{
    "_id": {
        "$oid": "571a43c6698c96cfb392bed0"
    },
    "ID": 2,
    "Room_ID": 303,
    "StartTime": "2016-03-12T10:00:00",
    "EndTime": "2016-03-12T11:00:00",
    "Login_ID": "ABCDE",
    "Status": "Active"
}

我的collection.update查询是:

server.put('/cancelReservation', function (req, res, next) {
    var user = req.params;
    if (req.params.SSO.trim().length == 0) {
        console.log("Inside if loop");
        res.writeHead(403, {
            'Content-Type': 'application/json; charset=utf-8'
        });
        res.end(JSON.stringify({
            error: "mServer.js: Invalid Session"
        }));
    }
    else {
        console.log("mServer.js: Inside cancelReservation SSO: " + user.SSO);
        console.log("mServer.js: Inside cancelReservation Romm ID: " + user.reservationID);

        db.RoomReservation.update({_id: user.reservationID}, {$set: {"Status": "Cancelled"}}, function (err, data) {
            if(err) {
                res.writeHead(403, {
                    'Content-Type': 'application/json; charset=utf-8'
                });
                res.end(JSON.stringify({
                    error: "mServer.js: Error occured during cancellation of room reservation"
                }));
                console.log("mServer.js: Reserved room cancellation error");
            }
            else {
                //var lData = data.toArray();
                //console.log("Data is: " + lData);
                res.writeHead(200, {
                    'Content-Type': 'application/json; charset=utf-8'
                });
                res.end(JSON.stringify(data));
                console.log("mServer.js: Success reserved room cancellation: " + JSON.stringify(data));
            }
        });
    }
});

我正在尝试将StatusActive更新为Cancelled。更新查询成功并打印以下消息:

mServer.js: Inside cancelReservation SSO: ABCDE
mServer.js: Inside cancelReservation Romm ID: 571a43c6698c96cfb392bed0
mServer.js: Success reserved room cancellation: {"ok":1,"nModified":0,"n":0,"lastOp":"0","electionId":"571834df69

但是mongodb中的文档没有得到更新。有人可以帮我找错吗?

提前致谢

0 个答案:

没有答案