var WorkstationSchema = new Schema({
tag: { type : String },
address : { type : String , unique : true, required : true },
status: { type : String , required : true },
});
var ProblemSchema = new Schema({
type: { type: Number, default: 0 },
status: { type: Number, default: 0 },
dateCreated: { type: String, trim: true, default: '' },
workstation: {type: Schema.ObjectId, ref: 'Workstation'},
});
conditions = {type: problemType, status: 0, 'workstation.address': remote64};
update = {status: 1};
ProblemSchema.findOneAndUpdate(conditions, update, options).populate('workstation', 'address').exec(function (err, problem) {
if(err){
//do something
} else {
console.log(problem);
}
});
这些是我的实体,我需要查找具有此地址的工作站的问题并更新问题状态。
我该怎么做?
最佳答案
您可以应用没有workstation.address
的匹配条件来查找问题并进行填充,然后在匹配workstation.address
之后更新状态。
conditions = {type: problemType, status: 0};
ProblemSchema.find(conditions).populate("workstation", "address").exec(function(error, docs){
docs.forEach(function (problem) {
if(problem.workstation && problem.workstation.address === remote64) {
problem.status = 1;
problem.save(function(err, doc) {
if(err){
//do something
} else {
console.log(doc);
}
});
}
});
});
关于node.js - Mongoose :FindOneAndUpdate(),带有来自字段引用的查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39648391/