如何在express.js中编写findOneAndUpdate查询?

时间:2016-10-08 05:41:15

标签: mongodb express mongoose

我已经显示了我的数据,这些数据存储在数据库中

{
    "_id": {
        "$oid": "5799995943d643600fabd6b7"
    },
    "Username": "xx",
    "Email": "xx@gmail.com",
    "Info": "Deactivate",
    "Description": "aajdjdjddjdkjddjdjdhdj",
    "VerificationCode": "594565",
    "VerificationExpires": {
        "$date": "2016-10-07T10:20:20.077Z"
    }

}

我的控制器:

如果用户名,电子邮件,信息匹配,我需要更新"信息='有效' "这是在我需要删除' VerificationCode'字段和' VerificationExpires'我怎么能实现这个目标?

exports.updatearticle = function(req, res) {
    Article.findOneAndUpdate(
        { "Username":'xx', "Email":'xx@gmail.com', "Info": "Deactivate" },
        { "$set": { "Info": "Active" } },
        { "new": true }
        function (err, doc) {
            if (err) { // err: any errors that occurred
                console.log(err);
            } else { // doc: the document before updates are applied if `new: false`
                console.log(doc); // , the document returned after updates if `new  true`
                console.log(doc.Info);
            }

        }
    );
};

以上condtion匹配和信息变更但我想删除VerificationCode,VerificationExpires有人帮助我

1 个答案:

答案 0 :(得分:0)

    exports.updatearticle = function(req, res) {
    Article.findOne( { "Username":'xx', "Email":'xx@gmail.com', "Info": "Deactivate" }, function(err, result){
       if (!err && result) {
        result.Info = "Active"; // update ur values goes here
        result.VerificationCode = "";
        result.VerificationExpires = {};
        var article = new Article(result);
        article.save(function(err, result2){
            if(!err) {
               res.send(result2);
            } else res.send(err);
        })  

       } else res.send(err);
    });  
  }


 home this may help