【问题标题】:mongo update using arrayFilters and identifier results in "cannot use the part to traverse the element"mongo 使用 arrayFilters 和标识符更新导致“无法使用该部分遍历元素”
【发布时间】: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


【解决方案1】:

两个问题:

  1. 您确定此版本的 MongoDB 支持过滤位置运算符吗? This page建议你需要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/

【讨论】:

    猜你喜欢
    • 2018-11-11
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多