gpt4 book ai didi

node.js - Mongoose:FindOneAndUpdate(),带有来自字段引用的查询

转载 作者:太空宇宙 更新时间:2023-11-04 00:34:39 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com