【发布时间】:2018-07-01 15:16:06
【问题描述】:
按照文档here,我正在尝试更新是否满足条件并且出现上述错误。我有一个对象数组,如果满足条件,我想更新嵌套对象的特定字段。这是我目前所拥有的:
示例文档
"userImages" : [
{
"profilePicture" : "https://myprofilepic.jpg"
},
{
"favoritePicture" : "https://myfavepic.jpg"
}
]
代码
const imageToChange = 'https://noLongerValidImage';
const query = { _id };
const updateQuery = {
'$set': {
'userImages.$[elem].profilePicture': 'https://newProfilePic.png'
}
};
const options = {
"multi":false,
"upsert":false,
"arrayFilters":[{
"elem.profilePicture":imageToChange
}]
}
我没有发现任何问题,这本身就很麻烦,因为我无法解决这个问题:
不能使用部分(userImages.$[elem].profilePicture的userImages) 遍历元素 ({userImages: [ { profilePicture: “https://myprofilepic.jpg”,最喜欢的图片: "https://myprofilepic.jpg" }
这是正在运行的查询:
db.collection.update(
query,
updateQuery,
options
)
【问题讨论】:
-
你用的是什么版本的mongodb?你能完整的查询吗?
-
@Saravana 3.4.10
-
你能发布你正在尝试的完整查询吗?
-
@Saravana 抱歉耽搁了,我已经更新了帖子,正在运行查询。
标签: mongodb