问题描述
我有一个数据集
var JobSchema = Schema({
candidates: [{
user: { type: Schema.Types.ObjectId, ref: 'User'},
status: { type: String, default: 'In Progress' },
}]
});
我想找到一个特定的job's _id
并更新一个特定的候选人字段,例如{ _id: 123, user: 12345, status: "In Progress" }
I want to find a specific job's _id
and update a specific candidates' field such as { _id: 123, user: 12345, status: "In Progress" }
此操作的网址是---> localhost:3000/apply/:job_id/user_id
The url for this operation is ---> localhost:3000/apply/:job_id/user_id
例如
假设这是作业mongodb中当前保存的数据
Let say that this is the current saved data in job mongodb
{
"_id" : 123, ---> job_id
"candidates" :
[{
"_id" : 234 ,
"user": 345 , --> user_id
"status" : "In Progress"
},
{
"_id" : 345 ,
"user": 678 , --> user_id
"status" : "In Progress"
}]
"__v" : 0
}
我如何只更新特定字段,比如说在mongodb中属于某个候选人的_id
的status
字段,这是我的尝试
How do i update only a specific field let say status
field that belong to a certain candidates' _id
in mongodb, Here's my attempt
Job.update(
{
_id: req.params.job_id,
},
{
$set: {'candidates.$.status': 'Accepted'} ,
}, function(err, count) {
if (err) return next(err);
callback(err, count);
});
如果使用上述操作,我将收到此错误.
I will get this error If I use the above operation.
MongoError:位置运算符未从查询中找到所需的匹配项.未扩展的更新:候选人.$.status
MongoError: The positional operator did not find the match needed from the query. Unexpanded update: candidates.$.status
推荐答案
$
是解决方案:
$
is the solution:
Job.update(
{
_id: found._id,
'candidates.user': req.params.user_id
},
{
$set: { 'candidates.$.status': 'Accepted'} },
}, function(err, count) {
if (err) return next(err);
callback(err, count);
});
这篇关于如何更新猫鼬中的特定字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!