【发布时间】: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) } ]
【问题讨论】: