如何解决Mongoose Nodejs - 用增量值更新对象数组
我对 nodejs 很陌生。目前,我正在将一个值增加到一个数组对象中。编写此代码后出现错误。我想增加 linkview
的值。我发现了与我的问题 Mongoose - Increment a value inside an array of objects 类似的问题,但我仍然无法解决它。请帮忙
所以这是 User
架构
{
"_id": "67324b2cc6817758118e9557d8","name": "James","__v": 0,"affiliatelink": [
{
"storeId": 60014d6e7286763490c3543,"storeName": white choc,"linkview": 1
}
]
}`
这是我的代码,错误是 MongoError: The positional operator did not find the match needed from the query
Admin.findByIdAndUpdate({ _id: adminId },{$inc: {"affiliatelink.$.linkview": 1}},function(err,result) {
console.log("ni error "+err)
//if (err) return next(err)
});
解决方法
您需要在对象数组 affiliateLink
您可以使用以下查询并试一试:
Admin.findOneAndUpdate(
{
_id: adminId,"affiliateLink.storeId": "60014d6e7286763490c3543"
},{
$inc: {
"affiliateLink.$.linkView": 1
}
},function(err,result) {
console.log("ni error "+err)
//if (err) return next(err)
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。