【问题标题】:In Mogoose i found my document with find, but using update not在猫鼬中,我用 find 找到了我的文档,但没有使用更新
【发布时间】:2017-11-16 15:18:07
【问题描述】:

我正在使用 nodejs 和 mongo (with mongooose) 来构建一个简单的应用程序,但是我遇到了这个小问题。

如果我使用查找方法搜索我的文档,我找到了该文档(是的,我可以在其中更新),但是如果我使用更新方法,则找不到它。为什么?

我的控制器方法

var query = {
        _id: ObjectId(req.params.runner_id)
    };

    Runner.find(query, function(e,o) {
        console.log(o); //here i found
    })

    Runner.update(query, req.body, function (err, qtd) {
        console.log(qtd); //here note

        if (err) {
            ...
        } else {
            ...
        }
    })

我的架构

module.exports = mongoose.model("Runner", new Schema({
    cellphone: {
        type: String,
        require: "qual o telefone?",
        unique: true,
        validate: {
            validator: function (v) {
                var re = /^\d{11}$/;
                return (v == null || v.trim().length < 1) || re.test(v);
            },
            message: "telefone inválido"
        }
    },
    created_at: {
        type: Date,
        default: Date.now
    },
    advisors: [{
        name: {
            type: String,
            require: "entre com o nome"
        },
        email: {
            type: String,
            require: "entre com o e-mail"
        },
        advisor: {
            type: ObjectId,
            require: "qual a assessoria?"
        }
    }]
}));

我的输出

更新 -> { ok: 0, n: 0, nModified: 0 } 用查找-> [ { _id: 5a0b99a9328fec0e4111ca52, 手机:'85999981114', __v: 0, 顾问:[[对象]], created_at: 2017 年 11 月 15 日星期三 01:34:33 GMT+0000 (UTC) } ]

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:
    app.get('/', function(req, res){
       var query = {
            _id: ObjectId(req.params.id)
        };
    
        Runner.find(query, function(e,o) {
            console.log(o); //here i found
        })
    }
    app.put('/:id', function(req, res){
        var Id=req.params.id;
     Runner.findById(Id, function (err, qtd) {
            console.log(qtd); //here qtd will found
    
            if (err) {
                ...
            } else {
                ...
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 2017-08-13
      • 2022-01-07
      • 2022-01-22
      • 1970-01-01
      • 2016-05-24
      相关资源
      最近更新 更多