Node.js mongoose更新子文档';设置为$s的属性无效
我想更新子文档的一些属性。 我的模型和更新功能如下所示:Node.js mongoose更新子文档';设置为$s的属性无效,node.js,mongoose,Node.js,Mongoose,我想更新子文档的一些属性。 我的模型和更新功能如下所示: const ParentSchema = mongoose.Schema({ ... subs : [SubSchema], ... }) const SubSchema = mongoose.Schema({ ... name : String, price : Number, ... }) const Pare
const ParentSchema = mongoose.Schema({
...
subs : [SubSchema],
...
})
const SubSchema = mongoose.Schema({
...
name : String,
price : Number,
...
})
const ParentModel = mongoose.model('Item', ItemSchema);
function updateSubDocument(parentId, subId, updateObj){
return ParentModel.update(
{'_id' : parentId, 'subs._id' : subId},
{
'$set' : {
'subs.$' : updateObj
}
},
{
new : false,
overwrite : true,
runValidators: true
}
).exec();
}
现在,当我试图更新特定子文档的某些属性时,mongoose会做两件奇怪的事情:
function updateSubDocument(parentId, subId, updateObj){
return ParentModel.update(
{'_id' : parentId, 'subs' : {$elemMatch:{_id:subId}},
{
'$set' : {
'subs.$.name' : updateObj.name,
'subs.$.price' : updateObj.price,
......
}
},
{
new : false,
overwrite : true,
runValidators: true
}
).exec();
}