使用数组过滤器和标识符进行 mongo 更新会导致"cannot use the part to traverse the element"



按照这里的文档,我正在尝试更新是否满足条件并收到上述错误。我有一个对象数组,如果满足条件,我想更新嵌套对象的特定字段。这是我到目前为止所拥有的:

示例文档

"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 of userImages.$[elem].profilePicture( 遍历元素 ({userImages: [ { profilePicture: "https://myprofilepic.jpg",收藏图片: "https://myprofilepic.jpg" }

下面是正在运行的查询:

db.collection.update(
   query,
   updateQuery,
   options
)

两个问题:

  1. 你确定这个版本的MongoDB支持过滤的位置运算符吗?此页面建议您需要 3.6

  2. featureCompatibilityVersion变量的值是多少?

    > db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
    { "featureCompatibilityVersion" : { "version" : "3.4" }, "ok" : 1 }`
    

为了设置它运行:

    > db.adminCommand( { setFeatureCompatibilityVersion: "3.4" })

下面介绍了版本之间的不兼容性:https://docs.mongodb.com/manual/release-notes/3.4-compatibility/

最新更新